VirtualBox

source: vbox/trunk/src/VBox/Devices/Input/PS2M.cpp@ 69922

Last change on this file since 69922 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 46.2 KB
Line 
1/* $Id: PS2M.cpp 69500 2017-10-28 15:14:05Z vboxsync $ */
2/** @file
3 * PS2M - PS/2 auxiliary device (mouse) emulation.
4 */
5
6/*
7 * Copyright (C) 2007-2017 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 *
56 * Standard PS/2 pointing device three-byte report packet format:
57 *
58 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
59 * |Bit/byte| bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
60 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
61 * | Byte 1 | Y ovfl | X ovfl | Y sign | X sign | Sync | M btn | R btn | L btn |
62 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
63 * | Byte 2 | X movement delta (two's complement) |
64 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
65 * | Byte 3 | Y movement delta (two's complement) |
66 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
67 *
68 * - The sync bit is always set. It allows software to synchronize data packets
69 * as the X/Y position data typically does not have bit 4 set.
70 * - The overflow bits are set if motion exceeds accumulator range. We use the
71 * maximum range (effectively 9 bits) and do not set the overflow bits.
72 * - Movement in the up/right direction is defined as having positive sign.
73 *
74 *
75 * IntelliMouse PS/2 (ImPS/2) fourth report packet byte:
76 *
77 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
78 * |Bit/byte| bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
79 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
80 * | Byte 4 | Z movement delta (two's complement) |
81 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
82 *
83 * - The valid range for Z delta values is only -8/+7, i.e. 4 bits.
84 *
85 * IntelliMouse Explorer (ImEx) fourth report packet byte:
86 *
87 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
88 * |Bit/byte| bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
89 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
90 * | Byte 4 | 0 | 0 | Btn 5 | Btn 4 | Z mov't delta (two's complement) |
91 * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
92 *
93 */
94
95
96/*********************************************************************************************************************************
97* Header Files *
98*********************************************************************************************************************************/
99#define LOG_GROUP LOG_GROUP_DEV_KBD
100#include <VBox/vmm/pdmdev.h>
101#include <VBox/err.h>
102#include <iprt/assert.h>
103#include <iprt/uuid.h>
104#include "VBoxDD.h"
105#define IN_PS2M
106#include "PS2Dev.h"
107
108
109/*********************************************************************************************************************************
110* Defined Constants And Macros *
111*********************************************************************************************************************************/
112/** @name Auxiliary device commands sent by the system.
113 * @{ */
114#define ACMD_SET_SCALE_11 0xE6 /* Set 1:1 scaling. */
115#define ACMD_SET_SCALE_21 0xE7 /* Set 2:1 scaling. */
116#define ACMD_SET_RES 0xE8 /* Set resolution. */
117#define ACMD_REQ_STATUS 0xE9 /* Get device status. */
118#define ACMD_SET_STREAM 0xEA /* Set stream mode. */
119#define ACMD_READ_REMOTE 0xEB /* Read remote data. */
120#define ACMD_RESET_WRAP 0xEC /* Exit wrap mode. */
121#define ACMD_INVALID_1 0xED
122#define ACMD_SET_WRAP 0xEE /* Set wrap (echo) mode. */
123#define ACMD_INVALID_2 0xEF
124#define ACMD_SET_REMOTE 0xF0 /* Set remote mode. */
125#define ACMD_INVALID_3 0xF1
126#define ACMD_READ_ID 0xF2 /* Read device ID. */
127#define ACMD_SET_SAMP_RATE 0xF3 /* Set sampling rate. */
128#define ACMD_ENABLE 0xF4 /* Enable (streaming mode). */
129#define ACMD_DISABLE 0xF5 /* Disable (streaming mode). */
130#define ACMD_SET_DEFAULT 0xF6 /* Set defaults. */
131#define ACMD_INVALID_4 0xF7
132#define ACMD_INVALID_5 0xF8
133#define ACMD_INVALID_6 0xF9
134#define ACMD_INVALID_7 0xFA
135#define ACMD_INVALID_8 0xFB
136#define ACMD_INVALID_9 0xFC
137#define ACMD_INVALID_10 0xFD
138#define ACMD_RESEND 0xFE /* Resend response. */
139#define ACMD_RESET 0xFF /* Reset device. */
140/** @} */
141
142/** @name Auxiliary device responses sent to the system.
143 * @{ */
144#define ARSP_ID 0x00
145#define ARSP_BAT_OK 0xAA /* Self-test passed. */
146#define ARSP_ACK 0xFA /* Command acknowledged. */
147#define ARSP_ERROR 0xFC /* Bad command. */
148#define ARSP_RESEND 0xFE /* Requesting resend. */
149/** @} */
150
151/** Define a simple PS/2 input device queue. */
152#define DEF_PS2Q_TYPE(name, size) \
153 typedef struct { \
154 uint32_t rpos; \
155 uint32_t wpos; \
156 uint32_t cUsed; \
157 uint32_t cSize; \
158 uint8_t abQueue[size]; \
159 } name
160
161/* Internal mouse queue sizes. The input queue is relatively large,
162 * but the command queue only needs to handle a few bytes.
163 */
164#define AUX_EVT_QUEUE_SIZE 256
165#define AUX_CMD_QUEUE_SIZE 8
166
167
168/*********************************************************************************************************************************
169* Structures and Typedefs *
170*********************************************************************************************************************************/
171
172DEF_PS2Q_TYPE(AuxEvtQ, AUX_EVT_QUEUE_SIZE);
173DEF_PS2Q_TYPE(AuxCmdQ, AUX_CMD_QUEUE_SIZE);
174#ifndef VBOX_DEVICE_STRUCT_TESTCASE /// @todo hack
175DEF_PS2Q_TYPE(GeneriQ, 1);
176#endif
177
178/* Auxiliary device special modes of operation. */
179typedef enum {
180 AUX_MODE_STD, /* Standard operation. */
181 AUX_MODE_RESET, /* Currently in reset. */
182 AUX_MODE_WRAP /* Wrap mode (echoing input). */
183} PS2M_MODE;
184
185/* Auxiliary device operational state. */
186typedef enum {
187 AUX_STATE_RATE_ERR = RT_BIT(0), /* Invalid rate received. */
188 AUX_STATE_RES_ERR = RT_BIT(1), /* Invalid resolution received. */
189 AUX_STATE_SCALING = RT_BIT(4), /* 2:1 scaling in effect. */
190 AUX_STATE_ENABLED = RT_BIT(5), /* Reporting enabled in stream mode. */
191 AUX_STATE_REMOTE = RT_BIT(6) /* Remote mode (reports on request). */
192} PS2M_STATE;
193
194/* Externally visible state bits. */
195#define AUX_STATE_EXTERNAL (AUX_STATE_SCALING | AUX_STATE_ENABLED | AUX_STATE_REMOTE)
196
197/* Protocols supported by the PS/2 mouse. */
198typedef enum {
199 PS2M_PROTO_PS2STD = 0, /* Standard PS/2 mouse protocol. */
200 PS2M_PROTO_IMPS2 = 3, /* IntelliMouse PS/2 protocol. */
201 PS2M_PROTO_IMEX = 4 /* IntelliMouse Explorer protocol. */
202} PS2M_PROTO;
203
204/* Protocol selection 'knock' states. */
205typedef enum {
206 PS2M_KNOCK_INITIAL,
207 PS2M_KNOCK_1ST,
208 PS2M_KNOCK_IMPS2_2ND,
209 PS2M_KNOCK_IMEX_2ND
210} PS2M_KNOCK_STATE;
211
212/**
213 * The PS/2 auxiliary device instance data.
214 */
215typedef struct PS2M
216{
217 /** Pointer to parent device (keyboard controller). */
218 R3PTRTYPE(void *) pParent;
219 /** Operational state. */
220 uint8_t u8State;
221 /** Configured sampling rate. */
222 uint8_t u8SampleRate;
223 /** Configured resolution. */
224 uint8_t u8Resolution;
225 /** Currently processed command (if any). */
226 uint8_t u8CurrCmd;
227 /** Set if the throttle delay is active. */
228 bool fThrottleActive;
229 /** Set if the throttle delay is active. */
230 bool fDelayReset;
231 /** Operational mode. */
232 PS2M_MODE enmMode;
233 /** Currently used protocol. */
234 PS2M_PROTO enmProtocol;
235 /** Currently used protocol. */
236 PS2M_KNOCK_STATE enmKnockState;
237 /** Buffer holding mouse events to be sent to the host. */
238 AuxEvtQ evtQ;
239 /** Command response queue (priority). */
240 AuxCmdQ cmdQ;
241 /** Accumulated horizontal movement. */
242 int32_t iAccumX;
243 /** Accumulated vertical movement. */
244 int32_t iAccumY;
245 /** Accumulated Z axis movement. */
246 int32_t iAccumZ;
247 /** Accumulated button presses. */
248 uint32_t fAccumB;
249 /** Instantaneous button data. */
250 uint32_t fCurrB;
251 /** Button state last sent to the guest. */
252 uint32_t fReportedB;
253 /** Throttling delay in milliseconds. */
254 uint32_t uThrottleDelay;
255
256 /** The device critical section protecting everything - R3 Ptr */
257 R3PTRTYPE(PPDMCRITSECT) pCritSectR3;
258 /** Command delay timer - R3 Ptr. */
259 PTMTIMERR3 pDelayTimerR3;
260 /** Interrupt throttling timer - R3 Ptr. */
261 PTMTIMERR3 pThrottleTimerR3;
262 RTR3PTR Alignment1;
263
264 /** Command delay timer - RC Ptr. */
265 PTMTIMERRC pDelayTimerRC;
266 /** Interrupt throttling timer - RC Ptr. */
267 PTMTIMERRC pThrottleTimerRC;
268
269 /** Command delay timer - R0 Ptr. */
270 PTMTIMERR0 pDelayTimerR0;
271 /** Interrupt throttling timer - R0 Ptr. */
272 PTMTIMERR0 pThrottleTimerR0;
273
274 /**
275 * Mouse port - LUN#1.
276 *
277 * @implements PDMIBASE
278 * @implements PDMIMOUSEPORT
279 */
280 struct
281 {
282 /** The base interface for the mouse port. */
283 PDMIBASE IBase;
284 /** The keyboard port base interface. */
285 PDMIMOUSEPORT IPort;
286
287 /** The base interface of the attached mouse driver. */
288 R3PTRTYPE(PPDMIBASE) pDrvBase;
289 /** The keyboard interface of the attached mouse driver. */
290 R3PTRTYPE(PPDMIMOUSECONNECTOR) pDrv;
291 } Mouse;
292} PS2M, *PPS2M;
293
294AssertCompile(PS2M_STRUCT_FILLER >= sizeof(PS2M));
295
296#ifndef VBOX_DEVICE_STRUCT_TESTCASE
297
298
299/*********************************************************************************************************************************
300* Test code function declarations *
301*********************************************************************************************************************************/
302#if defined(RT_STRICT) && defined(IN_RING3)
303static void ps2mTestAccumulation(void);
304#endif
305
306
307/*********************************************************************************************************************************
308* Global Variables *
309*********************************************************************************************************************************/
310
311
312/*********************************************************************************************************************************
313* Internal Functions *
314*********************************************************************************************************************************/
315
316
317/**
318 * Clear a queue.
319 *
320 * @param pQ Pointer to the queue.
321 */
322static void ps2kClearQueue(GeneriQ *pQ)
323{
324 LogFlowFunc(("Clearing queue %p\n", pQ));
325 pQ->wpos = pQ->rpos;
326 pQ->cUsed = 0;
327}
328
329
330/**
331 * Add a byte to a queue.
332 *
333 * @param pQ Pointer to the queue.
334 * @param val The byte to store.
335 */
336static void ps2kInsertQueue(GeneriQ *pQ, uint8_t val)
337{
338 /* Check if queue is full. */
339 if (pQ->cUsed >= pQ->cSize)
340 {
341 LogRelFlowFunc(("queue %p full (%d entries)\n", pQ, pQ->cUsed));
342 return;
343 }
344 /* Insert data and update circular buffer write position. */
345 pQ->abQueue[pQ->wpos] = val;
346 if (++pQ->wpos == pQ->cSize)
347 pQ->wpos = 0; /* Roll over. */
348 ++pQ->cUsed;
349 LogRelFlowFunc(("inserted 0x%02X into queue %p\n", val, pQ));
350}
351
352#ifdef IN_RING3
353
354/**
355 * Save a queue state.
356 *
357 * @param pSSM SSM handle to write the state to.
358 * @param pQ Pointer to the queue.
359 */
360static void ps2kSaveQueue(PSSMHANDLE pSSM, GeneriQ *pQ)
361{
362 uint32_t cItems = pQ->cUsed;
363 int i;
364
365 /* Only save the number of items. Note that the read/write
366 * positions aren't saved as they will be rebuilt on load.
367 */
368 SSMR3PutU32(pSSM, cItems);
369
370 LogFlow(("Storing %d items from queue %p\n", cItems, pQ));
371
372 /* Save queue data - only the bytes actually used (typically zero). */
373 for (i = pQ->rpos; cItems-- > 0; i = (i + 1) % pQ->cSize)
374 SSMR3PutU8(pSSM, pQ->abQueue[i]);
375}
376
377/**
378 * Load a queue state.
379 *
380 * @param pSSM SSM handle to read the state from.
381 * @param pQ Pointer to the queue.
382 *
383 * @return int VBox status/error code.
384 */
385static int ps2kLoadQueue(PSSMHANDLE pSSM, GeneriQ *pQ)
386{
387 int rc;
388
389 /* On load, always put the read pointer at zero. */
390 SSMR3GetU32(pSSM, &pQ->cUsed);
391
392 LogFlow(("Loading %d items to queue %p\n", pQ->cUsed, pQ));
393
394 if (pQ->cUsed > pQ->cSize)
395 {
396 AssertMsgFailed(("Saved size=%u, actual=%u\n", pQ->cUsed, pQ->cSize));
397 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
398 }
399
400 /* Recalculate queue positions and load data in one go. */
401 pQ->rpos = 0;
402 pQ->wpos = pQ->cUsed;
403 rc = SSMR3GetMem(pSSM, pQ->abQueue, pQ->cUsed);
404
405 return rc;
406}
407
408/* Report a change in status down (or is it up?) the driver chain. */
409static void ps2mSetDriverState(PPS2M pThis, bool fEnabled)
410{
411 PPDMIMOUSECONNECTOR pDrv = pThis->Mouse.pDrv;
412 if (pDrv)
413 pDrv->pfnReportModes(pDrv, fEnabled, false, false);
414}
415
416/* Reset the pointing device. */
417static void ps2mReset(PPS2M pThis)
418{
419 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_BAT_OK);
420 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, 0);
421 pThis->enmMode = AUX_MODE_STD;
422 pThis->u8CurrCmd = 0;
423
424 /// @todo move to its proper home!
425 ps2mSetDriverState(pThis, true);
426}
427
428#endif /* IN_RING3 */
429
430/**
431 * Retrieve a byte from a queue.
432 *
433 * @param pQ Pointer to the queue.
434 * @param pVal Pointer to storage for the byte.
435 *
436 * @return int VINF_TRY_AGAIN if queue is empty,
437 * VINF_SUCCESS if a byte was read.
438 */
439static int ps2kRemoveQueue(GeneriQ *pQ, uint8_t *pVal)
440{
441 int rc = VINF_TRY_AGAIN;
442
443 Assert(pVal);
444 if (pQ->cUsed)
445 {
446 *pVal = pQ->abQueue[pQ->rpos];
447 if (++pQ->rpos == pQ->cSize)
448 pQ->rpos = 0; /* Roll over. */
449 --pQ->cUsed;
450 rc = VINF_SUCCESS;
451 LogFlowFunc(("removed 0x%02X from queue %p\n", *pVal, pQ));
452 } else
453 LogFlowFunc(("queue %p empty\n", pQ));
454 return rc;
455}
456
457static void ps2mSetRate(PPS2M pThis, uint8_t rate)
458{
459 Assert(rate);
460 pThis->uThrottleDelay = rate ? 1000 / rate : 0;
461 pThis->u8SampleRate = rate;
462 LogFlowFunc(("Sampling rate %u, throttle delay %u ms\n", pThis->u8SampleRate, pThis->uThrottleDelay));
463}
464
465static void ps2mSetDefaults(PPS2M pThis)
466{
467 LogFlowFunc(("Set mouse defaults\n"));
468 /* Standard protocol, reporting disabled, resolution 2, 1:1 scaling. */
469 pThis->enmProtocol = PS2M_PROTO_PS2STD;
470 pThis->u8State = 0;
471 pThis->u8Resolution = 2;
472
473 /* Sample rate 100 reports per second. */
474 ps2mSetRate(pThis, 100);
475
476 /* Event queue, eccumulators, and button status bits are cleared. */
477 ps2kClearQueue((GeneriQ *)&pThis->evtQ);
478 pThis->iAccumX = pThis->iAccumY = pThis->iAccumZ = pThis->fAccumB;
479}
480
481/* Handle the sampling rate 'knock' sequence which selects protocol. */
482static void ps2mRateProtocolKnock(PPS2M pThis, uint8_t rate)
483{
484 switch (pThis->enmKnockState)
485 {
486 case PS2M_KNOCK_INITIAL:
487 if (rate == 200)
488 pThis->enmKnockState = PS2M_KNOCK_1ST;
489 break;
490 case PS2M_KNOCK_1ST:
491 if (rate == 100)
492 pThis->enmKnockState = PS2M_KNOCK_IMPS2_2ND;
493 else if (rate == 200)
494 pThis->enmKnockState = PS2M_KNOCK_IMEX_2ND;
495 else
496 pThis->enmKnockState = PS2M_KNOCK_INITIAL;
497 break;
498 case PS2M_KNOCK_IMPS2_2ND:
499 if (rate == 80)
500 {
501 pThis->enmProtocol = PS2M_PROTO_IMPS2;
502 LogRelFlow(("PS2M: Switching mouse to ImPS/2 protocol.\n"));
503 }
504 pThis->enmKnockState = PS2M_KNOCK_INITIAL;
505 break;
506 case PS2M_KNOCK_IMEX_2ND:
507 if (rate == 80)
508 {
509 pThis->enmProtocol = PS2M_PROTO_IMEX;
510 LogRelFlow(("PS2M: Switching mouse to ImEx protocol.\n"));
511 }
512 RT_FALL_THRU();
513 default:
514 pThis->enmKnockState = PS2M_KNOCK_INITIAL;
515 }
516}
517
518/* Three-button event mask. */
519#define PS2M_STD_BTN_MASK (RT_BIT(0) | RT_BIT(1) | RT_BIT(2))
520
521/* Report accumulated movement and button presses, then clear the accumulators. */
522static void ps2mReportAccumulatedEvents(PPS2M pThis, GeneriQ *pQueue, bool fAccumBtns)
523{
524 uint32_t fBtnState = fAccumBtns ? pThis->fAccumB : pThis->fCurrB;
525 uint8_t val;
526 int dX, dY, dZ;
527
528 /* Clamp the accumulated delta values to the allowed range. */
529 dX = RT_MIN(RT_MAX(pThis->iAccumX, -255), 255);
530 dY = RT_MIN(RT_MAX(pThis->iAccumY, -255), 255);
531 dZ = RT_MIN(RT_MAX(pThis->iAccumZ, -8), 7);
532
533 /* Start with the sync bit and buttons 1-3. */
534 val = RT_BIT(3) | (fBtnState & PS2M_STD_BTN_MASK);
535 /* Set the X/Y sign bits. */
536 if (dX < 0)
537 val |= RT_BIT(4);
538 if (dY < 0)
539 val |= RT_BIT(5);
540
541 /* Send the standard 3-byte packet (always the same). */
542 ps2kInsertQueue(pQueue, val);
543 ps2kInsertQueue(pQueue, dX);
544 ps2kInsertQueue(pQueue, dY);
545
546 /* Add fourth byte if extended protocol is in use. */
547 if (pThis->enmProtocol > PS2M_PROTO_PS2STD)
548 {
549 if (pThis->enmProtocol == PS2M_PROTO_IMPS2)
550 ps2kInsertQueue(pQueue, dZ);
551 else
552 {
553 Assert(pThis->enmProtocol == PS2M_PROTO_IMEX);
554 /* Z value uses 4 bits; buttons 4/5 in bits 4 and 5. */
555 val = dZ & 0x0f;
556 val |= (fBtnState << 1) & (RT_BIT(4) | RT_BIT(5));
557 ps2kInsertQueue(pQueue, val);
558 }
559 }
560
561 /* Clear the movement accumulators, but not necessarily button state. */
562 pThis->iAccumX = pThis->iAccumY = pThis->iAccumZ = 0;
563 /* Clear accumulated button state only when it's being used. */
564 if (fAccumBtns)
565 {
566 pThis->fReportedB = pThis->fAccumB;
567 pThis->fAccumB = 0;
568 }
569}
570
571
572/* Determine whether a reporting rate is one of the valid ones. */
573bool ps2mIsRateSupported(uint8_t rate)
574{
575 static uint8_t aValidRates[] = { 10, 20, 40, 60, 80, 100, 200 };
576 size_t i;
577 bool fValid = false;
578
579 for (i = 0; i < RT_ELEMENTS(aValidRates); ++i)
580 if (aValidRates[i] == rate)
581 {
582 fValid = true;
583 break;
584 }
585
586 return fValid;
587}
588
589/**
590 * Receive and process a byte sent by the keyboard controller.
591 *
592 * @param pThis The PS/2 auxiliary device instance data.
593 * @param cmd The command (or data) byte.
594 */
595int PS2MByteToAux(PPS2M pThis, uint8_t cmd)
596{
597 uint8_t u8Val;
598 bool fHandled = true;
599
600 LogFlowFunc(("cmd=0x%02X, active cmd=0x%02X\n", cmd, pThis->u8CurrCmd));
601
602 if (pThis->enmMode == AUX_MODE_RESET)
603 /* In reset mode, do not respond at all. */
604 return VINF_SUCCESS;
605
606 /* If there's anything left in the command response queue, trash it. */
607 ps2kClearQueue((GeneriQ *)&pThis->cmdQ);
608
609 if (pThis->enmMode == AUX_MODE_WRAP)
610 {
611 /* In wrap mode, bounce most data right back.*/
612 if (cmd == ACMD_RESET || cmd == ACMD_RESET_WRAP)
613 ; /* Handle as regular commands. */
614 else
615 {
616 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, cmd);
617 return VINF_SUCCESS;
618 }
619 }
620
621#ifndef IN_RING3
622 /* Reset, Enable, and Set Default commands must be run in R3. */
623 if (cmd == ACMD_RESET || cmd == ACMD_ENABLE || cmd == ACMD_SET_DEFAULT)
624 return VINF_IOM_R3_IOPORT_WRITE;
625#endif
626
627 switch (cmd)
628 {
629 case ACMD_SET_SCALE_11:
630 pThis->u8State &= ~AUX_STATE_SCALING;
631 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
632 pThis->u8CurrCmd = 0;
633 break;
634 case ACMD_SET_SCALE_21:
635 pThis->u8State |= AUX_STATE_SCALING;
636 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
637 pThis->u8CurrCmd = 0;
638 break;
639 case ACMD_REQ_STATUS:
640 /* Report current status, sample rate, and resolution. */
641 u8Val = (pThis->u8State & AUX_STATE_EXTERNAL) | (pThis->fCurrB & PS2M_STD_BTN_MASK);
642 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
643 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, u8Val);
644 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, pThis->u8Resolution);
645 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, pThis->u8SampleRate);
646 pThis->u8CurrCmd = 0;
647 break;
648 case ACMD_SET_STREAM:
649 pThis->u8State &= ~AUX_STATE_REMOTE;
650 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
651 pThis->u8CurrCmd = 0;
652 break;
653 case ACMD_READ_REMOTE:
654 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
655 ps2mReportAccumulatedEvents(pThis, (GeneriQ *)&pThis->cmdQ, false);
656 pThis->u8CurrCmd = 0;
657 break;
658 case ACMD_RESET_WRAP:
659 pThis->enmMode = AUX_MODE_STD;
660 /* NB: Stream mode reporting remains disabled! */
661 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
662 pThis->u8CurrCmd = 0;
663 break;
664 case ACMD_SET_WRAP:
665 pThis->enmMode = AUX_MODE_WRAP;
666 pThis->u8State &= ~AUX_STATE_ENABLED;
667 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
668 pThis->u8CurrCmd = 0;
669 break;
670 case ACMD_SET_REMOTE:
671 pThis->u8State |= AUX_STATE_REMOTE;
672 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
673 pThis->u8CurrCmd = 0;
674 break;
675 case ACMD_READ_ID:
676 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
677 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, pThis->enmProtocol);
678 pThis->u8CurrCmd = 0;
679 break;
680 case ACMD_ENABLE:
681 pThis->u8State |= AUX_STATE_ENABLED;
682#ifdef IN_RING3
683 ps2mSetDriverState(pThis, true);
684#else
685 AssertLogRelMsgFailed(("Invalid ACMD_ENABLE outside R3!\n"));
686#endif
687 ps2kClearQueue((GeneriQ *)&pThis->evtQ);
688 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
689 pThis->u8CurrCmd = 0;
690 break;
691 case ACMD_DISABLE:
692 pThis->u8State &= ~AUX_STATE_ENABLED;
693 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
694 pThis->u8CurrCmd = 0;
695 break;
696 case ACMD_SET_DEFAULT:
697 ps2mSetDefaults(pThis);
698 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
699 pThis->u8CurrCmd = 0;
700 break;
701 case ACMD_RESEND:
702 pThis->u8CurrCmd = 0;
703 break;
704 case ACMD_RESET:
705 ps2mSetDefaults(pThis);
706 /// @todo reset more?
707 pThis->u8CurrCmd = cmd;
708 pThis->enmMode = AUX_MODE_RESET;
709 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
710 if (pThis->fDelayReset)
711 /* Slightly delay reset completion; it might take hundreds of ms. */
712 TMTimerSetMillies(pThis->CTX_SUFF(pDelayTimer), 1);
713 else
714#ifdef IN_RING3
715 ps2mReset(pThis);
716#else
717 AssertLogRelMsgFailed(("Invalid ACMD_RESET outside R3!\n"));
718#endif
719 break;
720 /* The following commands need a parameter. */
721 case ACMD_SET_RES:
722 case ACMD_SET_SAMP_RATE:
723 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
724 pThis->u8CurrCmd = cmd;
725 break;
726 default:
727 /* Sending a command instead of a parameter starts the new command. */
728 switch (pThis->u8CurrCmd)
729 {
730 case ACMD_SET_RES:
731 if (cmd < 4) /* Valid resolutions are 0-3. */
732 {
733 pThis->u8Resolution = cmd;
734 pThis->u8State &= ~AUX_STATE_RES_ERR;
735 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
736 pThis->u8CurrCmd = 0;
737 }
738 else
739 {
740 /* Bad resolution. Reply with Resend or Error. */
741 if (pThis->u8State & AUX_STATE_RES_ERR)
742 {
743 pThis->u8State &= ~AUX_STATE_RES_ERR;
744 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ERROR);
745 pThis->u8CurrCmd = 0;
746 }
747 else
748 {
749 pThis->u8State |= AUX_STATE_RES_ERR;
750 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_RESEND);
751 /* NB: Current command remains unchanged. */
752 }
753 }
754 break;
755 case ACMD_SET_SAMP_RATE:
756 if (ps2mIsRateSupported(cmd))
757 {
758 pThis->u8State &= ~AUX_STATE_RATE_ERR;
759 ps2mSetRate(pThis, cmd);
760 ps2mRateProtocolKnock(pThis, cmd);
761 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
762 pThis->u8CurrCmd = 0;
763 }
764 else
765 {
766 /* Bad rate. Reply with Resend or Error. */
767 if (pThis->u8State & AUX_STATE_RATE_ERR)
768 {
769 pThis->u8State &= ~AUX_STATE_RATE_ERR;
770 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ERROR);
771 pThis->u8CurrCmd = 0;
772 }
773 else
774 {
775 pThis->u8State |= AUX_STATE_RATE_ERR;
776 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_RESEND);
777 /* NB: Current command remains unchanged. */
778 }
779 }
780 break;
781 default:
782 fHandled = false;
783 }
784 /* Fall through only to handle unrecognized commands. */
785 if (fHandled)
786 break;
787 RT_FALL_THRU();
788
789 case ACMD_INVALID_1:
790 case ACMD_INVALID_2:
791 case ACMD_INVALID_3:
792 case ACMD_INVALID_4:
793 case ACMD_INVALID_5:
794 case ACMD_INVALID_6:
795 case ACMD_INVALID_7:
796 case ACMD_INVALID_8:
797 case ACMD_INVALID_9:
798 case ACMD_INVALID_10:
799 Log(("Unsupported command 0x%02X!\n", cmd));
800 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_RESEND);
801 pThis->u8CurrCmd = 0;
802 break;
803 }
804 LogFlowFunc(("Active cmd now 0x%02X; updating interrupts\n", pThis->u8CurrCmd));
805 return VINF_SUCCESS;
806}
807
808/**
809 * Send a byte (packet data or command response) to the keyboard controller.
810 *
811 * @returns VINF_SUCCESS or VINF_TRY_AGAIN.
812 * @param pThis The PS/2 auxiliary device instance data.
813 * @param pb Where to return the byte we've read.
814 * @remarks Caller must have entered the device critical section.
815 */
816int PS2MByteFromAux(PPS2M pThis, uint8_t *pb)
817{
818 int rc;
819
820 AssertPtr(pb);
821
822 /* Anything in the command queue has priority over data
823 * in the event queue. Additionally, packet data are
824 * blocked if a command is currently in progress, even if
825 * the command queue is empty.
826 */
827 /// @todo Probably should flush/not fill queue if stream mode reporting disabled?!
828 rc = ps2kRemoveQueue((GeneriQ *)&pThis->cmdQ, pb);
829 if (rc != VINF_SUCCESS && !pThis->u8CurrCmd && (pThis->u8State & AUX_STATE_ENABLED))
830 rc = ps2kRemoveQueue((GeneriQ *)&pThis->evtQ, pb);
831
832 LogFlowFunc(("mouse sends 0x%02x (%svalid data)\n", *pb, rc == VINF_SUCCESS ? "" : "not "));
833
834 return rc;
835}
836
837#ifdef IN_RING3
838
839/** Is there any state change to send as events to the guest? */
840static uint32_t ps2mHaveEvents(PPS2M pThis)
841{
842 return pThis->iAccumX | pThis->iAccumY | pThis->iAccumZ
843 | (pThis->fCurrB != pThis->fReportedB) | (pThis->fAccumB != 0);
844}
845
846/* Event rate throttling timer to emulate the auxiliary device sampling rate.
847 */
848static DECLCALLBACK(void) ps2mThrottleTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
849{
850 RT_NOREF2(pDevIns, pTimer);
851 PPS2M pThis = (PS2M *)pvUser;
852 uint32_t uHaveEvents;
853
854 /* Grab the lock to avoid races with PutEvent(). */
855 int rc = PDMCritSectEnter(pThis->pCritSectR3, VERR_SEM_BUSY);
856 AssertReleaseRC(rc);
857
858 /* If more movement is accumulated, report it and restart the timer. */
859 uHaveEvents = ps2mHaveEvents(pThis);
860 LogFlowFunc(("Have%s events\n", uHaveEvents ? "" : " no"));
861
862 if (uHaveEvents)
863 {
864 /* Report accumulated data, poke the KBC, and start the timer. */
865 ps2mReportAccumulatedEvents(pThis, (GeneriQ *)&pThis->evtQ, true);
866 KBCUpdateInterrupts(pThis->pParent);
867 TMTimerSetMillies(pThis->CTX_SUFF(pThrottleTimer), pThis->uThrottleDelay);
868 }
869 else
870 pThis->fThrottleActive = false;
871
872 PDMCritSectLeave(pThis->pCritSectR3);
873}
874
875/* The auxiliary device reset is specified to take up to about 500 milliseconds. We need
876 * to delay sending the result to the host for at least a tiny little while.
877 */
878static DECLCALLBACK(void) ps2mDelayTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
879{
880 RT_NOREF2(pDevIns, pTimer);
881 PPS2M pThis = (PS2M *)pvUser;
882
883 LogFlowFunc(("Delay timer: cmd %02X\n", pThis->u8CurrCmd));
884
885 Assert(pThis->u8CurrCmd == ACMD_RESET);
886 ps2mReset(pThis);
887
888 /// @todo Might want a PS2MCompleteCommand() to push last response, clear command, and kick the KBC...
889 /* Give the KBC a kick. */
890 KBCUpdateInterrupts(pThis->pParent);
891}
892
893
894/**
895 * Debug device info handler. Prints basic auxiliary device state.
896 *
897 * @param pDevIns Device instance which registered the info.
898 * @param pHlp Callback functions for doing output.
899 * @param pszArgs Argument string. Optional and specific to the handler.
900 */
901static DECLCALLBACK(void) ps2mInfoState(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
902{
903 static const char *pcszModes[] = { "normal", "reset", "wrap" };
904 static const char *pcszProtocols[] = { "PS/2", NULL, NULL, "ImPS/2", "ImEx" };
905 PPS2M pThis = KBDGetPS2MFromDevIns(pDevIns);
906 NOREF(pszArgs);
907
908 Assert(pThis->enmMode <= RT_ELEMENTS(pcszModes));
909 Assert(pThis->enmProtocol <= RT_ELEMENTS(pcszProtocols));
910 pHlp->pfnPrintf(pHlp, "PS/2 mouse state: %s, %s mode, reporting %s\n",
911 pcszModes[pThis->enmMode],
912 pThis->u8State & AUX_STATE_REMOTE ? "remote" : "stream",
913 pThis->u8State & AUX_STATE_ENABLED ? "enabled" : "disabled");
914 pHlp->pfnPrintf(pHlp, "Protocol: %s, scaling %u:1\n",
915 pcszProtocols[pThis->enmProtocol], pThis->u8State & AUX_STATE_SCALING ? 2 : 1);
916 pHlp->pfnPrintf(pHlp, "Active command %02X\n", pThis->u8CurrCmd);
917 pHlp->pfnPrintf(pHlp, "Sampling rate %u reports/sec, resolution %u counts/mm\n",
918 pThis->u8SampleRate, 1 << pThis->u8Resolution);
919 pHlp->pfnPrintf(pHlp, "Command queue: %d items (%d max)\n",
920 pThis->cmdQ.cUsed, pThis->cmdQ.cSize);
921 pHlp->pfnPrintf(pHlp, "Event queue : %d items (%d max)\n",
922 pThis->evtQ.cUsed, pThis->evtQ.cSize);
923}
924
925/* -=-=-=-=-=- Mouse: IBase -=-=-=-=-=- */
926
927/**
928 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
929 */
930static DECLCALLBACK(void *) ps2mQueryInterface(PPDMIBASE pInterface, const char *pszIID)
931{
932 PPS2M pThis = RT_FROM_MEMBER(pInterface, PS2M, Mouse.IBase);
933 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Mouse.IBase);
934 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThis->Mouse.IPort);
935 return NULL;
936}
937
938
939/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
940
941/**
942 * Mouse event handler.
943 *
944 * @returns VBox status code.
945 * @param pThis The PS/2 auxiliary device instance data.
946 * @param dx X direction movement delta.
947 * @param dy Y direction movement delta.
948 * @param dz Z (vertical scroll) movement delta.
949 * @param dw W (horizontal scroll) movement delta.
950 * @param fButtons Depressed button mask.
951 */
952static int ps2mPutEventWorker(PPS2M pThis, int32_t dx, int32_t dy,
953 int32_t dz, int32_t dw, uint32_t fButtons)
954{
955 RT_NOREF1(dw);
956 int rc = VINF_SUCCESS;
957
958 /* Update internal accumulators and button state. */
959 pThis->iAccumX += dx;
960 pThis->iAccumY += dy;
961 pThis->iAccumZ += dz;
962 pThis->fAccumB |= fButtons; /// @todo accumulate based on current protocol?
963 pThis->fCurrB = fButtons;
964
965 /* Report the event and start the throttle timer unless it's already running. */
966 if (!pThis->fThrottleActive)
967 {
968 ps2mReportAccumulatedEvents(pThis, (GeneriQ *)&pThis->evtQ, true);
969 KBCUpdateInterrupts(pThis->pParent);
970 pThis->fThrottleActive = true;
971 TMTimerSetMillies(pThis->CTX_SUFF(pThrottleTimer), pThis->uThrottleDelay);
972 }
973
974 return rc;
975}
976
977/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
978
979/**
980 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEvent}
981 */
982static DECLCALLBACK(int) ps2mPutEvent(PPDMIMOUSEPORT pInterface, int32_t dx, int32_t dy,
983 int32_t dz, int32_t dw, uint32_t fButtons)
984{
985 PPS2M pThis = RT_FROM_MEMBER(pInterface, PS2M, Mouse.IPort);
986 int rc = PDMCritSectEnter(pThis->pCritSectR3, VERR_SEM_BUSY);
987 AssertReleaseRC(rc);
988
989 LogRelFlowFunc(("dX=%d dY=%d dZ=%d dW=%d buttons=%02X\n", dx, dy, dz, dw, fButtons));
990 /* NB: The PS/2 Y axis direction is inverted relative to ours. */
991 ps2mPutEventWorker(pThis, dx, -dy, dz, dw, fButtons);
992
993 PDMCritSectLeave(pThis->pCritSectR3);
994 return VINF_SUCCESS;
995}
996
997/**
998 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEventAbs}
999 */
1000static DECLCALLBACK(int) ps2mPutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t x, uint32_t y,
1001 int32_t dz, int32_t dw, uint32_t fButtons)
1002{
1003 AssertFailedReturn(VERR_NOT_SUPPORTED);
1004 NOREF(pInterface); NOREF(x); NOREF(y); NOREF(dz); NOREF(dw); NOREF(fButtons);
1005}
1006
1007/**
1008 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEventMultiTouch}
1009 */
1010static DECLCALLBACK(int) ps2mPutEventMT(PPDMIMOUSEPORT pInterface, uint8_t cContacts,
1011 const uint64_t *pau64Contacts, uint32_t u32ScanTime)
1012{
1013 AssertFailedReturn(VERR_NOT_SUPPORTED);
1014 NOREF(pInterface); NOREF(cContacts); NOREF(pau64Contacts); NOREF(u32ScanTime);
1015}
1016
1017
1018
1019/**
1020 * Attach command.
1021 *
1022 * This is called to let the device attach to a driver for a
1023 * specified LUN.
1024 *
1025 * This is like plugging in the mouse after turning on the
1026 * system.
1027 *
1028 * @returns VBox status code.
1029 * @param pThis The PS/2 auxiliary device instance data.
1030 * @param pDevIns The device instance.
1031 * @param iLUN The logical unit which is being detached.
1032 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1033 */
1034int PS2MAttach(PPS2M pThis, PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
1035{
1036 int rc;
1037
1038 /* The LUN must be 1, i.e. mouse. */
1039 Assert(iLUN == 1);
1040 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
1041 ("PS/2 mouse does not support hotplugging\n"),
1042 VERR_INVALID_PARAMETER);
1043
1044 LogFlowFunc(("iLUN=%d\n", iLUN));
1045
1046 rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThis->Mouse.IBase, &pThis->Mouse.pDrvBase, "Mouse Port");
1047 if (RT_SUCCESS(rc))
1048 {
1049 pThis->Mouse.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Mouse.pDrvBase, PDMIMOUSECONNECTOR);
1050 if (!pThis->Mouse.pDrv)
1051 {
1052 AssertLogRelMsgFailed(("LUN #1 doesn't have a mouse interface! rc=%Rrc\n", rc));
1053 rc = VERR_PDM_MISSING_INTERFACE;
1054 }
1055 }
1056 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
1057 {
1058 Log(("%s/%d: warning: no driver attached to LUN #1!\n", pDevIns->pReg->szName, pDevIns->iInstance));
1059 rc = VINF_SUCCESS;
1060 }
1061 else
1062 AssertLogRelMsgFailed(("Failed to attach LUN #1! rc=%Rrc\n", rc));
1063
1064 return rc;
1065}
1066
1067void PS2MSaveState(PPS2M pThis, PSSMHANDLE pSSM)
1068{
1069 LogFlowFunc(("Saving PS2M state\n"));
1070
1071 /* Save the core auxiliary device state. */
1072 SSMR3PutU8(pSSM, pThis->u8State);
1073 SSMR3PutU8(pSSM, pThis->u8SampleRate);
1074 SSMR3PutU8(pSSM, pThis->u8Resolution);
1075 SSMR3PutU8(pSSM, pThis->u8CurrCmd);
1076 SSMR3PutU8(pSSM, pThis->enmMode);
1077 SSMR3PutU8(pSSM, pThis->enmProtocol);
1078 SSMR3PutU8(pSSM, pThis->enmKnockState);
1079
1080 /* Save the command and event queues. */
1081 ps2kSaveQueue(pSSM, (GeneriQ *)&pThis->cmdQ);
1082 ps2kSaveQueue(pSSM, (GeneriQ *)&pThis->evtQ);
1083
1084 /* Save the command delay timer. Note that the rate throttling
1085 * timer is *not* saved.
1086 */
1087 TMR3TimerSave(pThis->CTX_SUFF(pDelayTimer), pSSM);
1088}
1089
1090int PS2MLoadState(PPS2M pThis, PSSMHANDLE pSSM, uint32_t uVersion)
1091{
1092 uint8_t u8;
1093 int rc;
1094
1095 NOREF(uVersion);
1096 LogFlowFunc(("Loading PS2M state version %u\n", uVersion));
1097
1098 /* Load the basic auxiliary device state. */
1099 SSMR3GetU8(pSSM, &pThis->u8State);
1100 SSMR3GetU8(pSSM, &pThis->u8SampleRate);
1101 SSMR3GetU8(pSSM, &pThis->u8Resolution);
1102 SSMR3GetU8(pSSM, &pThis->u8CurrCmd);
1103 SSMR3GetU8(pSSM, &u8);
1104 pThis->enmMode = (PS2M_MODE)u8;
1105 SSMR3GetU8(pSSM, &u8);
1106 pThis->enmProtocol = (PS2M_PROTO)u8;
1107 SSMR3GetU8(pSSM, &u8);
1108 pThis->enmKnockState = (PS2M_KNOCK_STATE)u8;
1109
1110 /* Load the command and event queues. */
1111 rc = ps2kLoadQueue(pSSM, (GeneriQ *)&pThis->cmdQ);
1112 AssertRCReturn(rc, rc);
1113 rc = ps2kLoadQueue(pSSM, (GeneriQ *)&pThis->evtQ);
1114 AssertRCReturn(rc, rc);
1115
1116 /* Load the command delay timer, just in case. */
1117 rc = TMR3TimerLoad(pThis->CTX_SUFF(pDelayTimer), pSSM);
1118 AssertRCReturn(rc, rc);
1119
1120 /* Recalculate the throttling delay. */
1121 ps2mSetRate(pThis, pThis->u8SampleRate);
1122
1123 ps2mSetDriverState(pThis, !!(pThis->u8State & AUX_STATE_ENABLED));
1124
1125 return rc;
1126}
1127
1128void PS2MFixupState(PPS2M pThis, uint8_t u8State, uint8_t u8Rate, uint8_t u8Proto)
1129{
1130 LogFlowFunc(("Fixing up old PS2M state version\n"));
1131
1132 /* Load the basic auxiliary device state. */
1133 pThis->u8State = u8State;
1134 pThis->u8SampleRate = u8Rate ? u8Rate : 40; /* In case it wasn't saved right. */
1135 pThis->enmProtocol = (PS2M_PROTO)u8Proto;
1136
1137 /* Recalculate the throttling delay. */
1138 ps2mSetRate(pThis, pThis->u8SampleRate);
1139
1140 ps2mSetDriverState(pThis, !!(pThis->u8State & AUX_STATE_ENABLED));
1141}
1142
1143void PS2MReset(PPS2M pThis)
1144{
1145 LogFlowFunc(("Resetting PS2M\n"));
1146
1147 pThis->u8CurrCmd = 0;
1148
1149 /* Clear the queues. */
1150 ps2kClearQueue((GeneriQ *)&pThis->cmdQ);
1151 ps2mSetDefaults(pThis); /* Also clears event queue. */
1152}
1153
1154void PS2MRelocate(PPS2M pThis, RTGCINTPTR offDelta, PPDMDEVINS pDevIns)
1155{
1156 RT_NOREF2(pDevIns, offDelta);
1157 LogFlowFunc(("Relocating PS2M\n"));
1158 pThis->pDelayTimerRC = TMTimerRCPtr(pThis->pDelayTimerR3);
1159 pThis->pThrottleTimerRC = TMTimerRCPtr(pThis->pThrottleTimerR3);
1160}
1161
1162int PS2MConstruct(PPS2M pThis, PPDMDEVINS pDevIns, void *pParent, int iInstance)
1163{
1164 RT_NOREF1(iInstance);
1165
1166 LogFlowFunc(("iInstance=%d\n", iInstance));
1167
1168#ifdef RT_STRICT
1169 ps2mTestAccumulation();
1170#endif
1171
1172 pThis->pParent = pParent;
1173
1174 /* Initialize the queues. */
1175 pThis->evtQ.cSize = AUX_EVT_QUEUE_SIZE;
1176 pThis->cmdQ.cSize = AUX_CMD_QUEUE_SIZE;
1177
1178 pThis->Mouse.IBase.pfnQueryInterface = ps2mQueryInterface;
1179 pThis->Mouse.IPort.pfnPutEvent = ps2mPutEvent;
1180 pThis->Mouse.IPort.pfnPutEventAbs = ps2mPutEventAbs;
1181 pThis->Mouse.IPort.pfnPutEventMultiTouch = ps2mPutEventMT;
1182
1183 /*
1184 * Initialize the critical section pointer(s).
1185 */
1186 pThis->pCritSectR3 = pDevIns->pCritSectRoR3;
1187
1188 /*
1189 * Create the input rate throttling timer. Does not use virtual time!
1190 */
1191 PTMTIMER pTimer;
1192 int rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_REAL, ps2mThrottleTimer, pThis,
1193 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2M Throttle Timer", &pTimer);
1194 if (RT_FAILURE(rc))
1195 return rc;
1196
1197 pThis->pThrottleTimerR3 = pTimer;
1198 pThis->pThrottleTimerR0 = TMTimerR0Ptr(pTimer);
1199 pThis->pThrottleTimerRC = TMTimerRCPtr(pTimer);
1200
1201 /*
1202 * Create the command delay timer.
1203 */
1204 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ps2mDelayTimer, pThis,
1205 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2M Delay Timer", &pTimer);
1206 if (RT_FAILURE(rc))
1207 return rc;
1208
1209 pThis->pDelayTimerR3 = pTimer;
1210 pThis->pDelayTimerR0 = TMTimerR0Ptr(pTimer);
1211 pThis->pDelayTimerRC = TMTimerRCPtr(pTimer);
1212
1213 /*
1214 * Register debugger info callbacks.
1215 */
1216 PDMDevHlpDBGFInfoRegister(pDevIns, "ps2m", "Display PS/2 mouse state.", ps2mInfoState);
1217
1218 /// @todo Where should we do this?
1219 ps2mSetDriverState(pThis, true);
1220 pThis->u8State = 0;
1221 pThis->enmMode = AUX_MODE_STD;
1222
1223 return rc;
1224}
1225
1226#endif
1227
1228#if defined(RT_STRICT) && defined(IN_RING3)
1229/* -=-=-=-=-=- Test code -=-=-=-=-=- */
1230
1231/** Test the event accumulation mechanism which we use to delay events going
1232 * to the guest to one per 10ms (the default PS/2 mouse event rate). This
1233 * test depends on ps2mPutEventWorker() not touching the timer if
1234 * This.fThrottleActive is true. */
1235/** @todo if we add any more tests it might be worth using a table of test
1236 * operations and checks. */
1237static void ps2mTestAccumulation(void)
1238{
1239 PS2M This;
1240 unsigned i;
1241 int rc;
1242 uint8_t b;
1243
1244 RT_ZERO(This);
1245 This.evtQ.cSize = AUX_EVT_QUEUE_SIZE;
1246 This.u8State = AUX_STATE_ENABLED;
1247 This.fThrottleActive = true;
1248 /* Certain Windows touch pad drivers report a double tap as a press, then
1249 * a release-press-release all within a single 10ms interval. Simulate
1250 * this to check that it is handled right. */
1251 ps2mPutEventWorker(&This, 0, 0, 0, 0, 1);
1252 if (ps2mHaveEvents(&This))
1253 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1254 ps2mPutEventWorker(&This, 0, 0, 0, 0, 0);
1255 if (ps2mHaveEvents(&This))
1256 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1257 ps2mPutEventWorker(&This, 0, 0, 0, 0, 1);
1258 ps2mPutEventWorker(&This, 0, 0, 0, 0, 0);
1259 if (ps2mHaveEvents(&This))
1260 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1261 if (ps2mHaveEvents(&This))
1262 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1263 for (i = 0; i < 12; ++i)
1264 {
1265 const uint8_t abExpected[] = { 9, 0, 0, 8, 0, 0, 9, 0, 0, 8, 0, 0};
1266
1267 rc = PS2MByteFromAux(&This, &b);
1268 AssertRCSuccess(rc);
1269 Assert(b == abExpected[i]);
1270 }
1271 rc = PS2MByteFromAux(&This, &b);
1272 Assert(rc != VINF_SUCCESS);
1273 /* Button hold down during mouse drags was broken at some point during
1274 * testing fixes for the previous issue. Test that that works. */
1275 ps2mPutEventWorker(&This, 0, 0, 0, 0, 1);
1276 if (ps2mHaveEvents(&This))
1277 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1278 if (ps2mHaveEvents(&This))
1279 ps2mReportAccumulatedEvents(&This, (GeneriQ *)&This.evtQ, true);
1280 for (i = 0; i < 3; ++i)
1281 {
1282 const uint8_t abExpected[] = { 9, 0, 0 };
1283
1284 rc = PS2MByteFromAux(&This, &b);
1285 AssertRCSuccess(rc);
1286 Assert(b == abExpected[i]);
1287 }
1288 rc = PS2MByteFromAux(&This, &b);
1289 Assert(rc != VINF_SUCCESS);
1290}
1291#endif /* RT_STRICT && IN_RING3 */
1292
1293#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
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