VirtualBox

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

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

DevPS2: Discard input when device's serial line is disabled. Also discard queued mouse input if protocol changes.

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

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