VirtualBox

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

最後變更 在這個檔案從83301是 83301,由 vboxsync 提交於 5 年 前

Doxygen.

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

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