VirtualBox

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

Last change on this file since 89825 was 87820, checked in by vboxsync, 4 years ago

DevPS2M: The timer callbacks implicitly takes the device critical section before doing the callback.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 44.5 KB
Line 
1/* $Id: DevPS2M.cpp 87820 2021-02-20 10:25:39Z vboxsync $ */
2/** @file
3 * PS2M - PS/2 auxiliary device (mouse) emulation.
4 */
5
6/*
7 * Copyright (C) 2007-2020 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 LogFlowFunc(("Reset"));
222
223 PS2Q_INSERT(&pThis->cmdQ, ARSP_BAT_OK);
224 PS2Q_INSERT(&pThis->cmdQ, 0);
225 pThis->enmMode = AUX_MODE_STD;
226 pThis->u8CurrCmd = 0;
227
228 /// @todo move to its proper home!
229 ps2mR3SetDriverState(pThisCC, true);
230}
231
232#endif /* IN_RING3 */
233
234static void ps2mSetRate(PPS2M pThis, uint8_t rate)
235{
236 Assert(rate);
237 pThis->uThrottleDelay = rate ? 1000 / rate : 0;
238 pThis->u8SampleRate = rate;
239 LogFlowFunc(("Sampling rate %u, throttle delay %u ms\n", pThis->u8SampleRate, pThis->uThrottleDelay));
240}
241
242static void ps2mSetDefaults(PPS2M pThis)
243{
244 LogFlowFunc(("Set mouse defaults\n"));
245 /* Standard protocol, reporting disabled, resolution 2, 1:1 scaling. */
246 pThis->enmProtocol = PS2M_PROTO_PS2STD;
247 pThis->u8State = 0;
248 pThis->u8Resolution = 2;
249
250 /* Sample rate 100 reports per second. */
251 ps2mSetRate(pThis, 100);
252
253 /* Event queue, eccumulators, and button status bits are cleared. */
254 PS2Q_CLEAR(&pThis->evtQ);
255 pThis->iAccumX = pThis->iAccumY = pThis->iAccumZ = pThis->iAccumW = pThis->fAccumB = 0;
256}
257
258/* Handle the sampling rate 'knock' sequence which selects protocol. */
259static void ps2mRateProtocolKnock(PPS2M pThis, uint8_t rate)
260{
261 PS2M_PROTO enmOldProtocol = pThis->enmProtocol;
262 LogFlowFunc(("rate=%u\n", rate));
263
264 switch (pThis->enmKnockState)
265 {
266 case PS2M_KNOCK_INITIAL:
267 if (rate == 200)
268 pThis->enmKnockState = PS2M_KNOCK_1ST;
269 break;
270 case PS2M_KNOCK_1ST:
271 if (rate == 100)
272 pThis->enmKnockState = PS2M_KNOCK_IMPS2_2ND;
273 else if (rate == 200)
274 pThis->enmKnockState = PS2M_KNOCK_IMEX_2ND;
275 else if (rate == 80)
276 pThis->enmKnockState = PS2M_KNOCK_IMEX_HORZ_2ND;
277 else
278 pThis->enmKnockState = PS2M_KNOCK_INITIAL;
279 break;
280 case PS2M_KNOCK_IMPS2_2ND:
281 if (rate == 80)
282 {
283 pThis->enmProtocol = PS2M_PROTO_IMPS2;
284 LogRelFlow(("PS2M: Switching mouse to ImPS/2 protocol.\n"));
285 }
286 pThis->enmKnockState = PS2M_KNOCK_INITIAL;
287 break;
288 case PS2M_KNOCK_IMEX_2ND:
289 if (rate == 80)
290 {
291 pThis->enmProtocol = PS2M_PROTO_IMEX;
292 LogRelFlow(("PS2M: Switching mouse to ImEx protocol.\n"));
293 }
294 pThis->enmKnockState = PS2M_KNOCK_INITIAL;
295 break;
296 case PS2M_KNOCK_IMEX_HORZ_2ND:
297 if (rate == 40)
298 {
299 pThis->enmProtocol = PS2M_PROTO_IMEX_HORZ;
300 LogRelFlow(("PS2M: Switching mouse ImEx with horizontal scrolling.\n"));
301 }
302 RT_FALL_THRU();
303 default:
304 pThis->enmKnockState = PS2M_KNOCK_INITIAL;
305 }
306
307 /* If the protocol changed, throw away any queued input because it now
308 * has the wrong format, which could severely confuse the guest.
309 */
310 if (enmOldProtocol != pThis->enmProtocol)
311 PS2Q_CLEAR(&pThis->evtQ);
312}
313
314/* Three-button event mask. */
315#define PS2M_STD_BTN_MASK (RT_BIT(0) | RT_BIT(1) | RT_BIT(2))
316/* ImEx button 4/5 event mask. */
317#define PS2M_IMEX_BTN_MASK (RT_BIT(3) | RT_BIT(4))
318
319/** Report accumulated movement and button presses, then clear the accumulators. */
320static void ps2mReportAccumulatedEvents(PPS2M pThis, PPS2QHDR pQHdr, size_t cQElements, uint8_t *pbQElements, bool fAccumBtns)
321{
322 uint32_t fBtnState = fAccumBtns ? pThis->fAccumB : pThis->fCurrB;
323 uint8_t val;
324 int dX, dY, dZ, dW;
325
326 LogFlowFunc(("cQElements=%zu, fAccumBtns=%RTbool\n", cQElements, fAccumBtns));
327
328 /* Clamp the accumulated delta values to the allowed range. */
329 dX = RT_MIN(RT_MAX(pThis->iAccumX, -255), 255);
330 dY = RT_MIN(RT_MAX(pThis->iAccumY, -255), 255);
331
332 /* Start with the sync bit and buttons 1-3. */
333 val = RT_BIT(3) | (fBtnState & PS2M_STD_BTN_MASK);
334 /* Set the X/Y sign bits. */
335 if (dX < 0)
336 val |= RT_BIT(4);
337 if (dY < 0)
338 val |= RT_BIT(5);
339
340 /* Send the standard 3-byte packet (always the same). */
341 LogFlowFunc(("Queuing standard 3-byte packet\n"));
342 PS2CmnInsertQueue(pQHdr, cQElements, pbQElements, val);
343 PS2CmnInsertQueue(pQHdr, cQElements, pbQElements, dX);
344 PS2CmnInsertQueue(pQHdr, cQElements, pbQElements, dY);
345
346 /* Add fourth byte if an extended protocol is in use. */
347 if (pThis->enmProtocol > PS2M_PROTO_PS2STD)
348 {
349 /* Start out with 4-bit dZ range. */
350 dZ = RT_MIN(RT_MAX(pThis->iAccumZ, -8), 7);
351
352 if (pThis->enmProtocol == PS2M_PROTO_IMPS2)
353 {
354 /* NB: Only uses 4-bit dZ range, despite using a full byte. */
355 LogFlowFunc(("Queuing ImPS/2 last byte\n"));
356 PS2CmnInsertQueue(pQHdr, cQElements, pbQElements, dZ);
357 pThis->iAccumZ -= dZ;
358 }
359 else if (pThis->enmProtocol == PS2M_PROTO_IMEX)
360 {
361 /* Z value uses 4 bits; buttons 4/5 in bits 4 and 5. */
362 val = (fBtnState & PS2M_IMEX_BTN_MASK) << 1;
363 val |= dZ & 0x0f;
364 pThis->iAccumZ -= dZ;
365 LogFlowFunc(("Queuing ImEx last byte\n"));
366 PS2CmnInsertQueue(pQHdr, cQElements, pbQElements, val);
367 }
368 else
369 {
370 Assert((pThis->enmProtocol == PS2M_PROTO_IMEX_HORZ));
371 /* With ImEx + horizontal reporting, prioritize buttons 4/5. */
372 if (pThis->iAccumZ || pThis->iAccumW)
373 {
374 /* ImEx + horizontal reporting Horizontal scroll has
375 * precedence over vertical. Buttons cannot be reported
376 * this way.
377 */
378 if (pThis->iAccumW)
379 {
380 dW = RT_MIN(RT_MAX(pThis->iAccumW, -32), 31);
381 val = (dW & 0x3F) | 0x40;
382 pThis->iAccumW -= dW;
383 }
384 else
385 {
386 Assert(pThis->iAccumZ);
387 /* We can use 6-bit dZ range. Wow! */
388 dZ = RT_MIN(RT_MAX(pThis->iAccumZ, -32), 31);
389 val = (dZ & 0x3F) | 0x80;
390 pThis->iAccumZ -= dZ;
391 }
392 }
393 else
394 {
395 /* Just Buttons 4/5 in bits 4 and 5. No scrolling. */
396 val = (fBtnState & PS2M_IMEX_BTN_MASK) << 1;
397 }
398 LogFlowFunc(("Queuing ImEx+horz last byte\n"));
399 PS2CmnInsertQueue(pQHdr, cQElements, pbQElements, val);
400 }
401 }
402
403 /* Clear the movement accumulators, but not necessarily button state. */
404 pThis->iAccumX = pThis->iAccumY = 0;
405 /* Clear accumulated button state only when it's being used. */
406 if (fAccumBtns)
407 {
408 pThis->fReportedB = pThis->fCurrB | pThis->fAccumB;
409 pThis->fAccumB = 0;
410 }
411}
412
413
414/* Determine whether a reporting rate is one of the valid ones. */
415bool ps2mIsRateSupported(uint8_t rate)
416{
417 static uint8_t aValidRates[] = { 10, 20, 40, 60, 80, 100, 200 };
418 size_t i;
419 bool fValid = false;
420
421 for (i = 0; i < RT_ELEMENTS(aValidRates); ++i)
422 if (aValidRates[i] == rate)
423 {
424 fValid = true;
425 break;
426 }
427
428 return fValid;
429}
430
431
432/**
433 * The keyboard controller disabled the auxiliary serial line.
434 *
435 * @param pThis The PS/2 auxiliary device shared instance data.
436 */
437void PS2MLineDisable(PPS2M pThis)
438{
439 LogFlowFunc(("Disabling mouse serial line\n"));
440
441 pThis->fLineDisabled = true;
442}
443
444/**
445 * The keyboard controller enabled the auxiliary serial line.
446 *
447 * @param pThis The PS/2 auxiliary device shared instance data.
448 */
449void PS2MLineEnable(PPS2M pThis)
450{
451 LogFlowFunc(("Enabling mouse serial line\n"));
452
453 pThis->fLineDisabled = false;
454
455 /* If there was anything in the input queue,
456 * consider it lost and throw it away.
457 */
458 PS2Q_CLEAR(&pThis->evtQ);
459}
460
461
462/**
463 * Receive and process a byte sent by the keyboard controller.
464 *
465 * @param pDevIns The device instance.
466 * @param pThis The PS/2 auxiliary device shared instance data.
467 * @param cmd The command (or data) byte.
468 */
469int PS2MByteToAux(PPDMDEVINS pDevIns, PPS2M pThis, uint8_t cmd)
470{
471 uint8_t u8Val;
472 bool fHandled = true;
473
474 LogFlowFunc(("cmd=0x%02X, active cmd=0x%02X\n", cmd, pThis->u8CurrCmd));
475
476 if (pThis->enmMode == AUX_MODE_RESET)
477 /* In reset mode, do not respond at all. */
478 return VINF_SUCCESS;
479
480 /* If there's anything left in the command response queue, trash it. */
481 PS2Q_CLEAR(&pThis->cmdQ);
482
483 if (pThis->enmMode == AUX_MODE_WRAP)
484 {
485 /* In wrap mode, bounce most data right back.*/
486 if (cmd == ACMD_RESET || cmd == ACMD_RESET_WRAP)
487 ; /* Handle as regular commands. */
488 else
489 {
490 PS2Q_INSERT(&pThis->cmdQ, cmd);
491 return VINF_SUCCESS;
492 }
493 }
494
495#ifndef IN_RING3
496 /* Reset, Enable, and Set Default commands must be run in R3. */
497 if (cmd == ACMD_RESET || cmd == ACMD_ENABLE || cmd == ACMD_SET_DEFAULT)
498 return VINF_IOM_R3_IOPORT_WRITE;
499#endif
500
501 switch (cmd)
502 {
503 case ACMD_SET_SCALE_11:
504 pThis->u8State &= ~AUX_STATE_SCALING;
505 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
506 pThis->u8CurrCmd = 0;
507 break;
508 case ACMD_SET_SCALE_21:
509 pThis->u8State |= AUX_STATE_SCALING;
510 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
511 pThis->u8CurrCmd = 0;
512 break;
513 case ACMD_REQ_STATUS:
514 /* Report current status, sample rate, and resolution. */
515 u8Val = (pThis->u8State & AUX_STATE_EXTERNAL) | (pThis->fCurrB & PS2M_STD_BTN_MASK);
516 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
517 PS2Q_INSERT(&pThis->cmdQ, u8Val);
518 PS2Q_INSERT(&pThis->cmdQ, pThis->u8Resolution);
519 PS2Q_INSERT(&pThis->cmdQ, pThis->u8SampleRate);
520 pThis->u8CurrCmd = 0;
521 break;
522 case ACMD_SET_STREAM:
523 pThis->u8State &= ~AUX_STATE_REMOTE;
524 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
525 pThis->u8CurrCmd = 0;
526 break;
527 case ACMD_READ_REMOTE:
528 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
529 ps2mReportAccumulatedEvents(pThis, &pThis->cmdQ.Hdr, RT_ELEMENTS(pThis->cmdQ.abQueue), pThis->cmdQ.abQueue, false);
530 pThis->u8CurrCmd = 0;
531 break;
532 case ACMD_RESET_WRAP:
533 pThis->enmMode = AUX_MODE_STD;
534 /* NB: Stream mode reporting remains disabled! */
535 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
536 pThis->u8CurrCmd = 0;
537 break;
538 case ACMD_SET_WRAP:
539 pThis->enmMode = AUX_MODE_WRAP;
540 pThis->u8State &= ~AUX_STATE_ENABLED;
541 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
542 pThis->u8CurrCmd = 0;
543 break;
544 case ACMD_SET_REMOTE:
545 pThis->u8State |= AUX_STATE_REMOTE;
546 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
547 pThis->u8CurrCmd = 0;
548 break;
549 case ACMD_READ_ID:
550 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
551 /* ImEx + horizontal is protocol 4, just like plain ImEx. */
552 u8Val = pThis->enmProtocol == PS2M_PROTO_IMEX_HORZ ? PS2M_PROTO_IMEX : pThis->enmProtocol;
553 PS2Q_INSERT(&pThis->cmdQ, u8Val);
554 pThis->u8CurrCmd = 0;
555 break;
556 case ACMD_ENABLE:
557 pThis->u8State |= AUX_STATE_ENABLED;
558#ifdef IN_RING3
559 ps2mR3SetDriverState(&PDMDEVINS_2_DATA_CC(pDevIns, PKBDSTATER3)->Aux, true);
560#else
561 AssertLogRelMsgFailed(("Invalid ACMD_ENABLE outside R3!\n"));
562#endif
563 PS2Q_CLEAR(&pThis->evtQ);
564 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
565 pThis->u8CurrCmd = 0;
566 break;
567 case ACMD_DISABLE:
568 pThis->u8State &= ~AUX_STATE_ENABLED;
569 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
570 pThis->u8CurrCmd = 0;
571 break;
572 case ACMD_SET_DEFAULT:
573 ps2mSetDefaults(pThis);
574 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
575 pThis->u8CurrCmd = 0;
576 break;
577 case ACMD_RESEND:
578 pThis->u8CurrCmd = 0;
579 break;
580 case ACMD_RESET:
581 ps2mSetDefaults(pThis);
582 /// @todo reset more?
583 pThis->u8CurrCmd = cmd;
584 pThis->enmMode = AUX_MODE_RESET;
585 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
586 if (pThis->fDelayReset)
587 /* Slightly delay reset completion; it might take hundreds of ms. */
588 PDMDevHlpTimerSetMillies(pDevIns, pThis->hDelayTimer, 1);
589 else
590#ifdef IN_RING3
591 ps2mR3Reset(pThis, &PDMDEVINS_2_DATA_CC(pDevIns, PKBDSTATER3)->Aux);
592#else
593 AssertLogRelMsgFailed(("Invalid ACMD_RESET outside R3!\n"));
594#endif
595 break;
596 /* The following commands need a parameter. */
597 case ACMD_SET_RES:
598 case ACMD_SET_SAMP_RATE:
599 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
600 pThis->u8CurrCmd = cmd;
601 break;
602 default:
603 /* Sending a command instead of a parameter starts the new command. */
604 switch (pThis->u8CurrCmd)
605 {
606 case ACMD_SET_RES:
607 if (cmd < 4) /* Valid resolutions are 0-3. */
608 {
609 pThis->u8Resolution = cmd;
610 pThis->u8State &= ~AUX_STATE_RES_ERR;
611 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
612 pThis->u8CurrCmd = 0;
613 }
614 else
615 {
616 /* Bad resolution. Reply with Resend or Error. */
617 if (pThis->u8State & AUX_STATE_RES_ERR)
618 {
619 pThis->u8State &= ~AUX_STATE_RES_ERR;
620 PS2Q_INSERT(&pThis->cmdQ, ARSP_ERROR);
621 pThis->u8CurrCmd = 0;
622 }
623 else
624 {
625 pThis->u8State |= AUX_STATE_RES_ERR;
626 PS2Q_INSERT(&pThis->cmdQ, ARSP_RESEND);
627 /* NB: Current command remains unchanged. */
628 }
629 }
630 break;
631 case ACMD_SET_SAMP_RATE:
632 if (ps2mIsRateSupported(cmd))
633 {
634 pThis->u8State &= ~AUX_STATE_RATE_ERR;
635 ps2mSetRate(pThis, cmd);
636 ps2mRateProtocolKnock(pThis, cmd);
637 PS2Q_INSERT(&pThis->cmdQ, ARSP_ACK);
638 pThis->u8CurrCmd = 0;
639 }
640 else
641 {
642 /* Bad rate. Reply with Resend or Error. */
643 if (pThis->u8State & AUX_STATE_RATE_ERR)
644 {
645 pThis->u8State &= ~AUX_STATE_RATE_ERR;
646 PS2Q_INSERT(&pThis->cmdQ, ARSP_ERROR);
647 pThis->u8CurrCmd = 0;
648 }
649 else
650 {
651 pThis->u8State |= AUX_STATE_RATE_ERR;
652 PS2Q_INSERT(&pThis->cmdQ, ARSP_RESEND);
653 /* NB: Current command remains unchanged. */
654 }
655 }
656 break;
657 default:
658 fHandled = false;
659 }
660 /* Fall through only to handle unrecognized commands. */
661 if (fHandled)
662 break;
663 RT_FALL_THRU();
664
665 case ACMD_INVALID_1:
666 case ACMD_INVALID_2:
667 case ACMD_INVALID_3:
668 case ACMD_INVALID_4:
669 case ACMD_INVALID_5:
670 case ACMD_INVALID_6:
671 case ACMD_INVALID_7:
672 case ACMD_INVALID_8:
673 case ACMD_INVALID_9:
674 case ACMD_INVALID_10:
675 Log(("Unsupported command 0x%02X!\n", cmd));
676 PS2Q_INSERT(&pThis->cmdQ, ARSP_RESEND);
677 pThis->u8CurrCmd = 0;
678 break;
679 }
680 LogFlowFunc(("Active cmd now 0x%02X; updating interrupts\n", pThis->u8CurrCmd));
681 return VINF_SUCCESS;
682}
683
684/**
685 * Send a byte (packet data or command response) to the keyboard controller.
686 *
687 * @returns VINF_SUCCESS or VINF_TRY_AGAIN.
688 * @param pThis The PS/2 auxiliary device shared instance data.
689 * @param pb Where to return the byte we've read.
690 * @remarks Caller must have entered the device critical section.
691 */
692int PS2MByteFromAux(PPS2M pThis, uint8_t *pb)
693{
694 int rc;
695
696 AssertPtr(pb);
697
698 /* Anything in the command queue has priority over data
699 * in the event queue. Additionally, packet data are
700 * blocked if a command is currently in progress, even if
701 * the command queue is empty.
702 */
703 /// @todo Probably should flush/not fill queue if stream mode reporting disabled?!
704 rc = PS2Q_REMOVE(&pThis->cmdQ, pb);
705 if (rc != VINF_SUCCESS && !pThis->u8CurrCmd && (pThis->u8State & AUX_STATE_ENABLED))
706 rc = PS2Q_REMOVE(&pThis->evtQ, pb);
707
708 LogFlowFunc(("mouse sends 0x%02x (%svalid data)\n", *pb, rc == VINF_SUCCESS ? "" : "not "));
709
710 return rc;
711}
712
713#ifdef IN_RING3
714
715/** Is there any state change to send as events to the guest? */
716static uint32_t ps2mR3HaveEvents(PPS2M pThis)
717{
718/** @todo r=bird: Why is this returning uint32_t when you're calculating a
719 * boolean value here? Also, it's a predicate function... */
720 return pThis->iAccumX || pThis->iAccumY || pThis->iAccumZ || pThis->iAccumW
721 || ((pThis->fCurrB | pThis->fAccumB) != pThis->fReportedB);
722}
723
724/**
725 * @callback_method_impl{FNTMTIMERDEV,
726 * Event rate throttling timer to emulate the auxiliary device sampling rate.}
727 */
728static DECLCALLBACK(void) ps2mR3ThrottleTimer(PPDMDEVINS pDevIns, TMTIMERHANDLE hTimer, void *pvUser)
729{
730 PPS2M pThis = (PS2M *)pvUser;
731 uint32_t uHaveEvents;
732 Assert(hTimer == pThis->hThrottleTimer);
733 Assert(PDMDevHlpCritSectIsOwner(pDevIns, pDevIns->pCritSectRoR3));
734
735 /* If more movement is accumulated, report it and restart the timer. */
736 uHaveEvents = ps2mR3HaveEvents(pThis);
737 LogFlowFunc(("Have%s events\n", uHaveEvents ? "" : " no"));
738
739 if (uHaveEvents)
740 {
741 /* Report accumulated data, poke the KBC, and start the timer. */
742 ps2mReportAccumulatedEvents(pThis, &pThis->evtQ.Hdr, RT_ELEMENTS(pThis->evtQ.abQueue), pThis->evtQ.abQueue, true);
743 KBCUpdateInterrupts(pDevIns);
744 PDMDevHlpTimerSetMillies(pDevIns, hTimer, pThis->uThrottleDelay);
745 }
746 else
747 pThis->fThrottleActive = false;
748}
749
750/**
751 * @callback_method_impl{FNTMTIMERDEV}
752 *
753 * The auxiliary device reset is specified to take up to about 500 milliseconds.
754 * We need to delay sending the result to the host for at least a tiny little
755 * while.
756 */
757static DECLCALLBACK(void) ps2mR3DelayTimer(PPDMDEVINS pDevIns, TMTIMERHANDLE hTimer, void *pvUser)
758{
759 PPS2M pThis = &PDMDEVINS_2_DATA(pDevIns, PKBDSTATE)->Aux;
760 PPS2MR3 pThisCC = &PDMDEVINS_2_DATA_CC(pDevIns, PKBDSTATER3)->Aux;
761 RT_NOREF(pvUser, hTimer);
762
763 LogFlowFunc(("Delay timer: cmd %02X\n", pThis->u8CurrCmd));
764
765 Assert(pThis->u8CurrCmd == ACMD_RESET);
766 ps2mR3Reset(pThis, pThisCC);
767
768 /// @todo Might want a PS2MCompleteCommand() to push last response, clear command, and kick the KBC...
769 /* Give the KBC a kick. */
770 KBCUpdateInterrupts(pDevIns);
771}
772
773
774/**
775 * Debug device info handler. Prints basic auxiliary device state.
776 *
777 * @param pDevIns Device instance which registered the info.
778 * @param pHlp Callback functions for doing output.
779 * @param pszArgs Argument string. Optional and specific to the handler.
780 */
781static DECLCALLBACK(void) ps2mR3InfoState(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
782{
783 static const char * const s_apcszModes[] = { "normal", "reset", "wrap" };
784 static const char * const s_apcszProtocols[] = { "PS/2", NULL, NULL, "ImPS/2", "ImEx", "ImEx+horizontal" };
785 PKBDSTATE pParent = PDMDEVINS_2_DATA(pDevIns, PKBDSTATE);
786 PPS2M pThis = &pParent->Aux;
787 NOREF(pszArgs);
788
789 Assert(pThis->enmMode < RT_ELEMENTS(s_apcszModes));
790 pHlp->pfnPrintf(pHlp, "PS/2 mouse state: %s, %s mode, reporting %s, serial line %s\n",
791 s_apcszModes[pThis->enmMode],
792 pThis->u8State & AUX_STATE_REMOTE ? "remote" : "stream",
793 pThis->u8State & AUX_STATE_ENABLED ? "enabled" : "disabled",
794 pThis->fLineDisabled ? "disabled" : "enabled");
795 Assert(pThis->enmProtocol < RT_ELEMENTS(s_apcszProtocols));
796 pHlp->pfnPrintf(pHlp, "Protocol: %s, scaling %u:1\n",
797 s_apcszProtocols[pThis->enmProtocol],
798 pThis->u8State & AUX_STATE_SCALING ? 2 : 1);
799 pHlp->pfnPrintf(pHlp, "Active command %02X\n", pThis->u8CurrCmd);
800 pHlp->pfnPrintf(pHlp, "Sampling rate %u reports/sec, resolution %u counts/mm\n",
801 pThis->u8SampleRate, 1 << pThis->u8Resolution);
802 pHlp->pfnPrintf(pHlp, "Command queue: %d items (%d max)\n",
803 PS2Q_COUNT(&pThis->cmdQ), PS2Q_SIZE(&pThis->cmdQ));
804 pHlp->pfnPrintf(pHlp, "Event queue : %d items (%d max)\n",
805 PS2Q_COUNT(&pThis->evtQ), PS2Q_SIZE(&pThis->evtQ));
806}
807
808
809/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
810
811/**
812 * Mouse event handler.
813 *
814 * @returns VBox status code.
815 * @param pDevIns The device instance.
816 * @param pThis The PS/2 auxiliary device shared instance data.
817 * @param dx X direction movement delta.
818 * @param dy Y direction movement delta.
819 * @param dz Z (vertical scroll) movement delta.
820 * @param dw W (horizontal scroll) movement delta.
821 * @param fButtons Depressed button mask.
822 */
823static int ps2mR3PutEventWorker(PPDMDEVINS pDevIns, PPS2M pThis, int32_t dx, int32_t dy, int32_t dz, int32_t dw, uint32_t fButtons)
824{
825 LogFlowFunc(("dx=%d, dy=%d, dz=%d, dw=%d, fButtons=%X\n", dx, dy, dz, dw, fButtons));
826
827 /* Update internal accumulators and button state. Ignore any buttons beyond 5. */
828 pThis->iAccumX += dx;
829 pThis->iAccumY += dy;
830 pThis->iAccumZ += dz;
831 pThis->iAccumW += dw;
832 pThis->fCurrB = fButtons & (PS2M_STD_BTN_MASK | PS2M_IMEX_BTN_MASK);
833 pThis->fAccumB |= pThis->fCurrB;
834
835 /* Ditch accumulated data that can't be reported by the current protocol.
836 * This avoids sending phantom empty reports when un-reportable events
837 * are received.
838 */
839 if (pThis->enmProtocol < PS2M_PROTO_IMEX_HORZ)
840 pThis->iAccumW = 0; /* No horizontal scroll. */
841
842 if (pThis->enmProtocol < PS2M_PROTO_IMEX)
843 {
844 pThis->fAccumB &= PS2M_STD_BTN_MASK; /* Only buttons 1-3. */
845 pThis->fCurrB &= PS2M_STD_BTN_MASK;
846 }
847
848 if (pThis->enmProtocol < PS2M_PROTO_IMPS2)
849 pThis->iAccumZ = 0; /* No vertical scroll. */
850
851 /* Report the event (if any) and start the throttle timer unless it's already running. */
852 if (!pThis->fThrottleActive && ps2mR3HaveEvents(pThis))
853 {
854 ps2mReportAccumulatedEvents(pThis, &pThis->evtQ.Hdr, RT_ELEMENTS(pThis->evtQ.abQueue), pThis->evtQ.abQueue, true);
855 KBCUpdateInterrupts(pDevIns);
856 pThis->fThrottleActive = true;
857 PDMDevHlpTimerSetMillies(pDevIns, pThis->hThrottleTimer, pThis->uThrottleDelay);
858 }
859
860 return VINF_SUCCESS;
861}
862
863
864/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
865
866/**
867 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEvent}
868 */
869static DECLCALLBACK(int) ps2mR3MousePort_PutEvent(PPDMIMOUSEPORT pInterface, int32_t dx, int32_t dy,
870 int32_t dz, int32_t dw, uint32_t fButtons)
871{
872 PPS2MR3 pThisCC = RT_FROM_MEMBER(pInterface, PS2MR3, Mouse.IPort);
873 PPDMDEVINS pDevIns = pThisCC->pDevIns;
874 PPS2M pThis = &PDMDEVINS_2_DATA(pDevIns, PKBDSTATE)->Aux;
875 int rc = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_SEM_BUSY);
876 AssertReleaseRC(rc);
877
878 LogRelFlowFunc(("dX=%d dY=%d dZ=%d dW=%d buttons=%02X\n", dx, dy, dz, dw, fButtons));
879 /* NB: The PS/2 Y axis direction is inverted relative to ours. */
880 ps2mR3PutEventWorker(pDevIns, pThis, dx, -dy, dz, dw, fButtons);
881
882 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
883 return VINF_SUCCESS;
884}
885
886/**
887 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEventAbs}
888 */
889static DECLCALLBACK(int) ps2mR3MousePort_PutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t x, uint32_t y,
890 int32_t dz, int32_t dw, uint32_t fButtons)
891{
892 AssertFailedReturn(VERR_NOT_SUPPORTED);
893 NOREF(pInterface); NOREF(x); NOREF(y); NOREF(dz); NOREF(dw); NOREF(fButtons);
894}
895
896/**
897 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEventMultiTouch}
898 */
899static DECLCALLBACK(int) ps2mR3MousePort_PutEventMT(PPDMIMOUSEPORT pInterface, uint8_t cContacts,
900 const uint64_t *pau64Contacts, uint32_t u32ScanTime)
901{
902 AssertFailedReturn(VERR_NOT_SUPPORTED);
903 NOREF(pInterface); NOREF(cContacts); NOREF(pau64Contacts); NOREF(u32ScanTime);
904}
905
906
907/* -=-=-=-=-=- Mouse: IBase -=-=-=-=-=- */
908
909/**
910 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
911 */
912static DECLCALLBACK(void *) ps2mR3QueryInterface(PPDMIBASE pInterface, const char *pszIID)
913{
914 PPS2MR3 pThisCC = RT_FROM_MEMBER(pInterface, PS2MR3, Mouse.IBase);
915 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThisCC->Mouse.IBase);
916 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThisCC->Mouse.IPort);
917 return NULL;
918}
919
920
921/* -=-=-=-=-=- Device management -=-=-=-=-=- */
922
923/**
924 * Attach command.
925 *
926 * This is called to let the device attach to a driver for a
927 * specified LUN.
928 *
929 * This is like plugging in the mouse after turning on the
930 * system.
931 *
932 * @returns VBox status code.
933 * @param pDevIns The device instance.
934 * @param pThisCC The PS/2 auxiliary device instance data for ring-3.
935 * @param iLUN The logical unit which is being detached.
936 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
937 */
938int PS2MR3Attach(PPDMDEVINS pDevIns, PPS2MR3 pThisCC, unsigned iLUN, uint32_t fFlags)
939{
940 int rc;
941
942 /* The LUN must be 1, i.e. mouse. */
943 Assert(iLUN == 1);
944 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
945 ("PS/2 mouse does not support hotplugging\n"),
946 VERR_INVALID_PARAMETER);
947
948 LogFlowFunc(("iLUN=%d\n", iLUN));
949
950 rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThisCC->Mouse.IBase, &pThisCC->Mouse.pDrvBase, "Mouse Port");
951 if (RT_SUCCESS(rc))
952 {
953 pThisCC->Mouse.pDrv = PDMIBASE_QUERY_INTERFACE(pThisCC->Mouse.pDrvBase, PDMIMOUSECONNECTOR);
954 if (!pThisCC->Mouse.pDrv)
955 {
956 AssertLogRelMsgFailed(("LUN #1 doesn't have a mouse interface! rc=%Rrc\n", rc));
957 rc = VERR_PDM_MISSING_INTERFACE;
958 }
959 }
960 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
961 {
962 Log(("%s/%d: warning: no driver attached to LUN #1!\n", pDevIns->pReg->szName, pDevIns->iInstance));
963 rc = VINF_SUCCESS;
964 }
965 else
966 AssertLogRelMsgFailed(("Failed to attach LUN #1! rc=%Rrc\n", rc));
967
968 return rc;
969}
970
971void PS2MR3SaveState(PPDMDEVINS pDevIns, PPS2M pThis, PSSMHANDLE pSSM)
972{
973 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
974 LogFlowFunc(("Saving PS2M state\n"));
975
976 /* Save the core auxiliary device state. */
977 pHlp->pfnSSMPutU8(pSSM, pThis->u8State);
978 pHlp->pfnSSMPutU8(pSSM, pThis->u8SampleRate);
979 pHlp->pfnSSMPutU8(pSSM, pThis->u8Resolution);
980 pHlp->pfnSSMPutU8(pSSM, pThis->u8CurrCmd);
981 pHlp->pfnSSMPutU8(pSSM, pThis->enmMode);
982 pHlp->pfnSSMPutU8(pSSM, pThis->enmProtocol);
983 pHlp->pfnSSMPutU8(pSSM, pThis->enmKnockState);
984
985 /* Save the command and event queues. */
986 PS2Q_SAVE(pHlp, pSSM, &pThis->cmdQ);
987 PS2Q_SAVE(pHlp, pSSM, &pThis->evtQ);
988
989 /* Save the command delay timer. Note that the rate throttling
990 * timer is *not* saved.
991 */
992 PDMDevHlpTimerSave(pDevIns, pThis->hDelayTimer, pSSM);
993}
994
995int PS2MR3LoadState(PPDMDEVINS pDevIns, PPS2M pThis, PPS2MR3 pThisCC, PSSMHANDLE pSSM, uint32_t uVersion)
996{
997 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
998 uint8_t u8;
999 int rc;
1000
1001 NOREF(uVersion);
1002 LogFlowFunc(("Loading PS2M state version %u\n", uVersion));
1003
1004 /* Load the basic auxiliary device state. */
1005 pHlp->pfnSSMGetU8(pSSM, &pThis->u8State);
1006 pHlp->pfnSSMGetU8(pSSM, &pThis->u8SampleRate);
1007 pHlp->pfnSSMGetU8(pSSM, &pThis->u8Resolution);
1008 pHlp->pfnSSMGetU8(pSSM, &pThis->u8CurrCmd);
1009 pHlp->pfnSSMGetU8(pSSM, &u8);
1010 pThis->enmMode = (PS2M_MODE)u8;
1011 pHlp->pfnSSMGetU8(pSSM, &u8);
1012 pThis->enmProtocol = (PS2M_PROTO)u8;
1013 pHlp->pfnSSMGetU8(pSSM, &u8);
1014 pThis->enmKnockState = (PS2M_KNOCK_STATE)u8;
1015
1016 /* Load the command and event queues. */
1017 rc = PS2Q_LOAD(pHlp, pSSM, &pThis->cmdQ);
1018 AssertRCReturn(rc, rc);
1019 rc = PS2Q_LOAD(pHlp, pSSM, &pThis->evtQ);
1020 AssertRCReturn(rc, rc);
1021
1022 /* Load the command delay timer, just in case. */
1023 rc = PDMDevHlpTimerLoad(pDevIns, pThis->hDelayTimer, pSSM);
1024 AssertRCReturn(rc, rc);
1025
1026 /* Recalculate the throttling delay. */
1027 ps2mSetRate(pThis, pThis->u8SampleRate);
1028
1029 ps2mR3SetDriverState(pThisCC, !!(pThis->u8State & AUX_STATE_ENABLED));
1030
1031 return VINF_SUCCESS;
1032}
1033
1034void PS2MR3FixupState(PPS2M pThis, PPS2MR3 pThisCC, uint8_t u8State, uint8_t u8Rate, uint8_t u8Proto)
1035{
1036 LogFlowFunc(("Fixing up old PS2M state version\n"));
1037
1038 /* Load the basic auxiliary device state. */
1039 pThis->u8State = u8State;
1040 pThis->u8SampleRate = u8Rate ? u8Rate : 40; /* In case it wasn't saved right. */
1041 pThis->enmProtocol = (PS2M_PROTO)u8Proto;
1042
1043 /* Recalculate the throttling delay. */
1044 ps2mSetRate(pThis, pThis->u8SampleRate);
1045
1046 ps2mR3SetDriverState(pThisCC, !!(pThis->u8State & AUX_STATE_ENABLED));
1047}
1048
1049void PS2MR3Reset(PPS2M pThis)
1050{
1051 LogFlowFunc(("Resetting PS2M\n"));
1052
1053 pThis->u8CurrCmd = 0;
1054
1055 /* Clear the queues. */
1056 PS2Q_CLEAR(&pThis->cmdQ);
1057 ps2mSetDefaults(pThis); /* Also clears event queue. */
1058}
1059
1060int PS2MR3Construct(PPDMDEVINS pDevIns, PPS2M pThis, PPS2MR3 pThisCC)
1061{
1062 LogFlowFunc(("\n"));
1063
1064 pThis->cmdQ.Hdr.pszDesc = "Aux Cmd";
1065 pThis->evtQ.Hdr.pszDesc = "Aux Evt";
1066
1067#ifdef RT_STRICT
1068 ps2mR3TestAccumulation();
1069#endif
1070
1071 /*
1072 * Initialize the state.
1073 */
1074 pThisCC->pDevIns = pDevIns;
1075 pThisCC->Mouse.IBase.pfnQueryInterface = ps2mR3QueryInterface;
1076 pThisCC->Mouse.IPort.pfnPutEvent = ps2mR3MousePort_PutEvent;
1077 pThisCC->Mouse.IPort.pfnPutEventAbs = ps2mR3MousePort_PutEventAbs;
1078 pThisCC->Mouse.IPort.pfnPutEventMultiTouch = ps2mR3MousePort_PutEventMT;
1079
1080 /*
1081 * Create the input rate throttling timer. Does not use virtual time!
1082 */
1083 int rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_REAL, ps2mR3ThrottleTimer, pThis,
1084 TMTIMER_FLAGS_DEFAULT_CRIT_SECT | TMTIMER_FLAGS_NO_RING0,
1085 "PS2M Throttle", &pThis->hThrottleTimer);
1086 AssertRCReturn(rc, rc);
1087
1088 /*
1089 * Create the command delay timer.
1090 */
1091 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ps2mR3DelayTimer, pThis,
1092 TMTIMER_FLAGS_DEFAULT_CRIT_SECT | TMTIMER_FLAGS_RING0, "PS2M Delay", &pThis->hDelayTimer);
1093 AssertRCReturn(rc, rc);
1094
1095 /*
1096 * Register debugger info callbacks.
1097 */
1098 PDMDevHlpDBGFInfoRegister(pDevIns, "ps2m", "Display PS/2 mouse state.", ps2mR3InfoState);
1099
1100 /// @todo Where should we do this?
1101 ps2mR3SetDriverState(pThisCC, true);
1102 pThis->u8State = 0;
1103 pThis->enmMode = AUX_MODE_STD;
1104
1105 return rc;
1106}
1107
1108#endif
1109
1110#if defined(RT_STRICT) && defined(IN_RING3)
1111/* -=-=-=-=-=- Test code -=-=-=-=-=- */
1112
1113/** Test the event accumulation mechanism which we use to delay events going
1114 * to the guest to one per 10ms (the default PS/2 mouse event rate). This
1115 * test depends on ps2mR3PutEventWorker() not touching the timer if
1116 * This.fThrottleActive is true. */
1117/** @todo if we add any more tests it might be worth using a table of test
1118 * operations and checks. */
1119static void ps2mR3TestAccumulation(void)
1120{
1121 PS2M This;
1122 unsigned i;
1123 int rc;
1124 uint8_t b;
1125
1126 RT_ZERO(This);
1127 This.u8State = AUX_STATE_ENABLED;
1128 This.fThrottleActive = true;
1129 This.cmdQ.Hdr.pszDesc = "Test Aux Cmd";
1130 This.evtQ.Hdr.pszDesc = "Test Aux Evt";
1131 /* Certain Windows touch pad drivers report a double tap as a press, then
1132 * a release-press-release all within a single 10ms interval. Simulate
1133 * this to check that it is handled right. */
1134 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 1);
1135 if (ps2mR3HaveEvents(&This))
1136 ps2mReportAccumulatedEvents(&This, &This.evtQ.Hdr, RT_ELEMENTS(This.evtQ.abQueue), This.evtQ.abQueue, true);
1137 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 0);
1138 if (ps2mR3HaveEvents(&This))
1139 ps2mReportAccumulatedEvents(&This, &This.evtQ.Hdr, RT_ELEMENTS(This.evtQ.abQueue), This.evtQ.abQueue, true);
1140 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 1);
1141 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 0);
1142 if (ps2mR3HaveEvents(&This))
1143 ps2mReportAccumulatedEvents(&This, &This.evtQ.Hdr, RT_ELEMENTS(This.evtQ.abQueue), This.evtQ.abQueue, true);
1144 if (ps2mR3HaveEvents(&This))
1145 ps2mReportAccumulatedEvents(&This, &This.evtQ.Hdr, RT_ELEMENTS(This.evtQ.abQueue), This.evtQ.abQueue, true);
1146 for (i = 0; i < 12; ++i)
1147 {
1148 const uint8_t abExpected[] = { 9, 0, 0, 8, 0, 0, 9, 0, 0, 8, 0, 0};
1149
1150 rc = PS2MByteFromAux(&This, &b);
1151 AssertRCSuccess(rc);
1152 Assert(b == abExpected[i]);
1153 }
1154 rc = PS2MByteFromAux(&This, &b);
1155 Assert(rc != VINF_SUCCESS);
1156 /* Button hold down during mouse drags was broken at some point during
1157 * testing fixes for the previous issue. Test that that works. */
1158 ps2mR3PutEventWorker(NULL, &This, 0, 0, 0, 0, 1);
1159 if (ps2mR3HaveEvents(&This))
1160 ps2mReportAccumulatedEvents(&This, &This.evtQ.Hdr, RT_ELEMENTS(This.evtQ.abQueue), This.evtQ.abQueue, true);
1161 if (ps2mR3HaveEvents(&This))
1162 ps2mReportAccumulatedEvents(&This, &This.evtQ.Hdr, RT_ELEMENTS(This.evtQ.abQueue), This.evtQ.abQueue, true);
1163 for (i = 0; i < 3; ++i)
1164 {
1165 const uint8_t abExpected[] = { 9, 0, 0 };
1166
1167 rc = PS2MByteFromAux(&This, &b);
1168 AssertRCSuccess(rc);
1169 Assert(b == abExpected[i]);
1170 }
1171 rc = PS2MByteFromAux(&This, &b);
1172 Assert(rc != VINF_SUCCESS);
1173}
1174#endif /* RT_STRICT && IN_RING3 */
1175
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