VirtualBox

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

Last change on this file since 64468 was 63562, checked in by vboxsync, 8 years ago

scm: cleaning up todos

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 44.1 KB
Line 
1/* $Id: PS2M.cpp 63562 2016-08-16 14:04:03Z vboxsync $ */
2/** @file
3 * PS2M - PS/2 auxiliary device (mouse) emulation.
4 */
5
6/*
7 * Copyright (C) 2007-2016 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* Global Variables *
301*********************************************************************************************************************************/
302
303
304/*********************************************************************************************************************************
305* Internal Functions *
306*********************************************************************************************************************************/
307
308
309/**
310 * Clear a queue.
311 *
312 * @param pQ Pointer to the queue.
313 */
314static void ps2kClearQueue(GeneriQ *pQ)
315{
316 LogFlowFunc(("Clearing queue %p\n", pQ));
317 pQ->wpos = pQ->rpos;
318 pQ->cUsed = 0;
319}
320
321
322/**
323 * Add a byte to a queue.
324 *
325 * @param pQ Pointer to the queue.
326 * @param val The byte to store.
327 */
328static void ps2kInsertQueue(GeneriQ *pQ, uint8_t val)
329{
330 /* Check if queue is full. */
331 if (pQ->cUsed >= pQ->cSize)
332 {
333 LogRelFlowFunc(("queue %p full (%d entries)\n", pQ, pQ->cUsed));
334 return;
335 }
336 /* Insert data and update circular buffer write position. */
337 pQ->abQueue[pQ->wpos] = val;
338 if (++pQ->wpos == pQ->cSize)
339 pQ->wpos = 0; /* Roll over. */
340 ++pQ->cUsed;
341 LogRelFlowFunc(("inserted 0x%02X into queue %p\n", val, pQ));
342}
343
344#ifdef IN_RING3
345
346/**
347 * Save a queue state.
348 *
349 * @param pSSM SSM handle to write the state to.
350 * @param pQ Pointer to the queue.
351 */
352static void ps2kSaveQueue(PSSMHANDLE pSSM, GeneriQ *pQ)
353{
354 uint32_t cItems = pQ->cUsed;
355 int i;
356
357 /* Only save the number of items. Note that the read/write
358 * positions aren't saved as they will be rebuilt on load.
359 */
360 SSMR3PutU32(pSSM, cItems);
361
362 LogFlow(("Storing %d items from queue %p\n", cItems, pQ));
363
364 /* Save queue data - only the bytes actually used (typically zero). */
365 for (i = pQ->rpos; cItems-- > 0; i = (i + 1) % pQ->cSize)
366 SSMR3PutU8(pSSM, pQ->abQueue[i]);
367}
368
369/**
370 * Load a queue state.
371 *
372 * @param pSSM SSM handle to read the state from.
373 * @param pQ Pointer to the queue.
374 *
375 * @return int VBox status/error code.
376 */
377static int ps2kLoadQueue(PSSMHANDLE pSSM, GeneriQ *pQ)
378{
379 int rc;
380
381 /* On load, always put the read pointer at zero. */
382 SSMR3GetU32(pSSM, &pQ->cUsed);
383
384 LogFlow(("Loading %d items to queue %p\n", pQ->cUsed, pQ));
385
386 if (pQ->cUsed > pQ->cSize)
387 {
388 AssertMsgFailed(("Saved size=%u, actual=%u\n", pQ->cUsed, pQ->cSize));
389 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
390 }
391
392 /* Recalculate queue positions and load data in one go. */
393 pQ->rpos = 0;
394 pQ->wpos = pQ->cUsed;
395 rc = SSMR3GetMem(pSSM, pQ->abQueue, pQ->cUsed);
396
397 return rc;
398}
399
400/* Report a change in status down (or is it up?) the driver chain. */
401static void ps2mSetDriverState(PPS2M pThis, bool fEnabled)
402{
403 PPDMIMOUSECONNECTOR pDrv = pThis->Mouse.pDrv;
404 if (pDrv)
405 pDrv->pfnReportModes(pDrv, fEnabled, false, false);
406}
407
408/* Reset the pointing device. */
409static void ps2mReset(PPS2M pThis)
410{
411 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_BAT_OK);
412 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, 0);
413 pThis->enmMode = AUX_MODE_STD;
414 pThis->u8CurrCmd = 0;
415
416 /// @todo move to its proper home!
417 ps2mSetDriverState(pThis, true);
418}
419
420#endif /* IN_RING3 */
421
422/**
423 * Retrieve a byte from a queue.
424 *
425 * @param pQ Pointer to the queue.
426 * @param pVal Pointer to storage for the byte.
427 *
428 * @return int VINF_TRY_AGAIN if queue is empty,
429 * VINF_SUCCESS if a byte was read.
430 */
431static int ps2kRemoveQueue(GeneriQ *pQ, uint8_t *pVal)
432{
433 int rc = VINF_TRY_AGAIN;
434
435 Assert(pVal);
436 if (pQ->cUsed)
437 {
438 *pVal = pQ->abQueue[pQ->rpos];
439 if (++pQ->rpos == pQ->cSize)
440 pQ->rpos = 0; /* Roll over. */
441 --pQ->cUsed;
442 rc = VINF_SUCCESS;
443 LogFlowFunc(("removed 0x%02X from queue %p\n", *pVal, pQ));
444 } else
445 LogFlowFunc(("queue %p empty\n", pQ));
446 return rc;
447}
448
449static void ps2mSetRate(PPS2M pThis, uint8_t rate)
450{
451 Assert(rate);
452 pThis->uThrottleDelay = rate ? 1000 / rate : 0;
453 pThis->u8SampleRate = rate;
454 LogFlowFunc(("Sampling rate %u, throttle delay %u ms\n", pThis->u8SampleRate, pThis->uThrottleDelay));
455}
456
457static void ps2mSetDefaults(PPS2M pThis)
458{
459 LogFlowFunc(("Set mouse defaults\n"));
460 /* Standard protocol, reporting disabled, resolution 2, 1:1 scaling. */
461 pThis->enmProtocol = PS2M_PROTO_PS2STD;
462 pThis->u8State = 0;
463 pThis->u8Resolution = 2;
464
465 /* Sample rate 100 reports per second. */
466 ps2mSetRate(pThis, 100);
467
468 /* Event queue, eccumulators, and button status bits are cleared. */
469 ps2kClearQueue((GeneriQ *)&pThis->evtQ);
470 pThis->iAccumX = pThis->iAccumY = pThis->iAccumZ = pThis->fAccumB;
471}
472
473/* Handle the sampling rate 'knock' sequence which selects protocol. */
474static void ps2mRateProtocolKnock(PPS2M pThis, uint8_t rate)
475{
476 switch (pThis->enmKnockState)
477 {
478 case PS2M_KNOCK_INITIAL:
479 if (rate == 200)
480 pThis->enmKnockState = PS2M_KNOCK_1ST;
481 break;
482 case PS2M_KNOCK_1ST:
483 if (rate == 100)
484 pThis->enmKnockState = PS2M_KNOCK_IMPS2_2ND;
485 else if (rate == 200)
486 pThis->enmKnockState = PS2M_KNOCK_IMEX_2ND;
487 else
488 pThis->enmKnockState = PS2M_KNOCK_INITIAL;
489 break;
490 case PS2M_KNOCK_IMPS2_2ND:
491 if (rate == 80)
492 {
493 pThis->enmProtocol = PS2M_PROTO_IMPS2;
494 LogRelFlow(("PS2M: Switching mouse to ImPS/2 protocol.\n"));
495 }
496 pThis->enmKnockState = PS2M_KNOCK_INITIAL;
497 break;
498 case PS2M_KNOCK_IMEX_2ND:
499 if (rate == 80)
500 {
501 pThis->enmProtocol = PS2M_PROTO_IMEX;
502 LogRelFlow(("PS2M: Switching mouse to ImEx protocol.\n"));
503 }
504 /* Fall through! */
505 default:
506 pThis->enmKnockState = PS2M_KNOCK_INITIAL;
507 }
508}
509
510/* Three-button event mask. */
511#define PS2M_STD_BTN_MASK (RT_BIT(0) | RT_BIT(1) | RT_BIT(2))
512
513/* Report accumulated movement and button presses, then clear the accumulators. */
514static void ps2mReportAccumulatedEvents(PPS2M pThis, GeneriQ *pQueue, bool fAccumBtns)
515{
516 uint32_t fBtnState = fAccumBtns ? pThis->fAccumB : pThis->fCurrB;
517 uint8_t val;
518 int dX, dY, dZ;
519
520 /* Clamp the accumulated delta values to the allowed range. */
521 dX = RT_MIN(RT_MAX(pThis->iAccumX, -255), 255);
522 dY = RT_MIN(RT_MAX(pThis->iAccumY, -255), 255);
523 dZ = RT_MIN(RT_MAX(pThis->iAccumZ, -8), 7);
524
525 /* Start with the sync bit and buttons 1-3. */
526 val = RT_BIT(3) | (fBtnState & PS2M_STD_BTN_MASK);
527 /* Set the X/Y sign bits. */
528 if (dX < 0)
529 val |= RT_BIT(4);
530 if (dY < 0)
531 val |= RT_BIT(5);
532
533 /* Send the standard 3-byte packet (always the same). */
534 ps2kInsertQueue(pQueue, val);
535 ps2kInsertQueue(pQueue, dX);
536 ps2kInsertQueue(pQueue, dY);
537
538 /* Add fourth byte if extended protocol is in use. */
539 if (pThis->enmProtocol > PS2M_PROTO_PS2STD)
540 {
541 if (pThis->enmProtocol == PS2M_PROTO_IMPS2)
542 ps2kInsertQueue(pQueue, dZ);
543 else
544 {
545 Assert(pThis->enmProtocol == PS2M_PROTO_IMEX);
546 /* Z value uses 4 bits; buttons 4/5 in bits 4 and 5. */
547 val = dZ & 0x0f;
548 val |= (fBtnState << 1) & (RT_BIT(4) | RT_BIT(5));
549 ps2kInsertQueue(pQueue, val);
550 }
551 }
552
553 /* Clear the movement accumulators, but not necessarily button state. */
554 pThis->iAccumX = pThis->iAccumY = pThis->iAccumZ = 0;
555 /* Clear accumulated button state only when it's being used. */
556 if (fAccumBtns)
557 {
558 pThis->fReportedB = pThis->fAccumB;
559 pThis->fAccumB = 0;
560 }
561}
562
563
564/* Determine whether a reporting rate is one of the valid ones. */
565bool ps2mIsRateSupported(uint8_t rate)
566{
567 static uint8_t aValidRates[] = { 10, 20, 40, 60, 80, 100, 200 };
568 size_t i;
569 bool fValid = false;
570
571 for (i = 0; i < RT_ELEMENTS(aValidRates); ++i)
572 if (aValidRates[i] == rate)
573 {
574 fValid = true;
575 break;
576 }
577
578 return fValid;
579}
580
581/**
582 * Receive and process a byte sent by the keyboard controller.
583 *
584 * @param pThis The PS/2 auxiliary device instance data.
585 * @param cmd The command (or data) byte.
586 */
587int PS2MByteToAux(PPS2M pThis, uint8_t cmd)
588{
589 uint8_t u8Val;
590 bool fHandled = true;
591
592 LogFlowFunc(("cmd=0x%02X, active cmd=0x%02X\n", cmd, pThis->u8CurrCmd));
593//LogRel(("aux: cmd=0x%02X, active cmd=0x%02X\n", cmd, pThis->u8CurrCmd));
594
595 if (pThis->enmMode == AUX_MODE_RESET)
596 /* In reset mode, do not respond at all. */
597 return VINF_SUCCESS;
598
599 /* If there's anything left in the command response queue, trash it. */
600 ps2kClearQueue((GeneriQ *)&pThis->cmdQ);
601
602 if (pThis->enmMode == AUX_MODE_WRAP)
603 {
604 /* In wrap mode, bounce most data right back.*/
605 if (cmd == ACMD_RESET || cmd == ACMD_RESET_WRAP)
606 ; /* Handle as regular commands. */
607 else
608 {
609 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, cmd);
610 return VINF_SUCCESS;
611 }
612 }
613
614#ifndef IN_RING3
615 /* Reset, Enable, and Set Default commands must be run in R3. */
616 if (cmd == ACMD_RESET || cmd == ACMD_ENABLE || cmd == ACMD_SET_DEFAULT)
617 return VINF_IOM_R3_IOPORT_WRITE;
618#endif
619
620 switch (cmd)
621 {
622 case ACMD_SET_SCALE_11:
623 pThis->u8State &= ~AUX_STATE_SCALING;
624 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
625 pThis->u8CurrCmd = 0;
626 break;
627 case ACMD_SET_SCALE_21:
628 pThis->u8State |= AUX_STATE_SCALING;
629 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
630 pThis->u8CurrCmd = 0;
631 break;
632 case ACMD_REQ_STATUS:
633 /* Report current status, sample rate, and resolution. */
634 u8Val = (pThis->u8State & AUX_STATE_EXTERNAL) | (pThis->fCurrB & PS2M_STD_BTN_MASK);
635 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
636 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, u8Val);
637 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, pThis->u8Resolution);
638 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, pThis->u8SampleRate);
639 pThis->u8CurrCmd = 0;
640 break;
641 case ACMD_SET_STREAM:
642 pThis->u8State &= ~AUX_STATE_REMOTE;
643 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
644 pThis->u8CurrCmd = 0;
645 break;
646 case ACMD_READ_REMOTE:
647 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
648 ps2mReportAccumulatedEvents(pThis, (GeneriQ *)&pThis->cmdQ, false);
649 pThis->u8CurrCmd = 0;
650 break;
651 case ACMD_RESET_WRAP:
652 pThis->enmMode = AUX_MODE_STD;
653 /* NB: Stream mode reporting remains disabled! */
654 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
655 pThis->u8CurrCmd = 0;
656 break;
657 case ACMD_SET_WRAP:
658 pThis->enmMode = AUX_MODE_WRAP;
659 pThis->u8State &= ~AUX_STATE_ENABLED;
660 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
661 pThis->u8CurrCmd = 0;
662 break;
663 case ACMD_SET_REMOTE:
664 pThis->u8State |= AUX_STATE_REMOTE;
665 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
666 pThis->u8CurrCmd = 0;
667 break;
668 case ACMD_READ_ID:
669 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
670 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, pThis->enmProtocol);
671 pThis->u8CurrCmd = 0;
672 break;
673 case ACMD_ENABLE:
674 pThis->u8State |= AUX_STATE_ENABLED;
675#ifdef IN_RING3
676 ps2mSetDriverState(pThis, true);
677#else
678 AssertLogRelMsgFailed(("Invalid ACMD_ENABLE outside R3!\n"));
679#endif
680 ps2kClearQueue((GeneriQ *)&pThis->evtQ);
681 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
682 pThis->u8CurrCmd = 0;
683 break;
684 case ACMD_DISABLE:
685 pThis->u8State &= ~AUX_STATE_ENABLED;
686 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
687 pThis->u8CurrCmd = 0;
688 break;
689 case ACMD_SET_DEFAULT:
690 ps2mSetDefaults(pThis);
691 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
692 pThis->u8CurrCmd = 0;
693 break;
694 case ACMD_RESEND:
695 pThis->u8CurrCmd = 0;
696 break;
697 case ACMD_RESET:
698 ps2mSetDefaults(pThis);
699 /// @todo reset more?
700 pThis->u8CurrCmd = cmd;
701 pThis->enmMode = AUX_MODE_RESET;
702 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
703 if (pThis->fDelayReset)
704 /* Slightly delay reset completion; it might take hundreds of ms. */
705 TMTimerSetMillies(pThis->CTX_SUFF(pDelayTimer), 1);
706 else
707#ifdef IN_RING3
708 ps2mReset(pThis);
709#else
710 AssertLogRelMsgFailed(("Invalid ACMD_RESET outside R3!\n"));
711#endif
712 break;
713 /* The following commands need a parameter. */
714 case ACMD_SET_RES:
715 case ACMD_SET_SAMP_RATE:
716 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
717 pThis->u8CurrCmd = cmd;
718 break;
719 default:
720 /* Sending a command instead of a parameter starts the new command. */
721 switch (pThis->u8CurrCmd)
722 {
723 case ACMD_SET_RES:
724 if (cmd < 4) /* Valid resolutions are 0-3. */
725 {
726 pThis->u8Resolution = cmd;
727 pThis->u8State &= ~AUX_STATE_RES_ERR;
728 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
729 pThis->u8CurrCmd = 0;
730 }
731 else
732 {
733 /* Bad resolution. Reply with Resend or Error. */
734 if (pThis->u8State & AUX_STATE_RES_ERR)
735 {
736 pThis->u8State &= ~AUX_STATE_RES_ERR;
737 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ERROR);
738 pThis->u8CurrCmd = 0;
739 }
740 else
741 {
742 pThis->u8State |= AUX_STATE_RES_ERR;
743 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_RESEND);
744 /* NB: Current command remains unchanged. */
745 }
746 }
747 break;
748 case ACMD_SET_SAMP_RATE:
749 if (ps2mIsRateSupported(cmd))
750 {
751 pThis->u8State &= ~AUX_STATE_RATE_ERR;
752 ps2mSetRate(pThis, cmd);
753 ps2mRateProtocolKnock(pThis, cmd);
754 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
755 pThis->u8CurrCmd = 0;
756 }
757 else
758 {
759 /* Bad rate. Reply with Resend or Error. */
760 if (pThis->u8State & AUX_STATE_RATE_ERR)
761 {
762 pThis->u8State &= ~AUX_STATE_RATE_ERR;
763 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ERROR);
764 pThis->u8CurrCmd = 0;
765 }
766 else
767 {
768 pThis->u8State |= AUX_STATE_RATE_ERR;
769 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_RESEND);
770 /* NB: Current command remains unchanged. */
771 }
772 }
773 break;
774 default:
775 fHandled = false;
776 }
777 /* Fall through only to handle unrecognized commands. */
778 if (fHandled)
779 break;
780
781 case ACMD_INVALID_1:
782 case ACMD_INVALID_2:
783 case ACMD_INVALID_3:
784 case ACMD_INVALID_4:
785 case ACMD_INVALID_5:
786 case ACMD_INVALID_6:
787 case ACMD_INVALID_7:
788 case ACMD_INVALID_8:
789 case ACMD_INVALID_9:
790 case ACMD_INVALID_10:
791 Log(("Unsupported command 0x%02X!\n", cmd));
792 ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_RESEND);
793 pThis->u8CurrCmd = 0;
794 break;
795 }
796 LogFlowFunc(("Active cmd now 0x%02X; updating interrupts\n", pThis->u8CurrCmd));
797// KBCUpdateInterrupts(pThis->pParent);
798 return VINF_SUCCESS;
799}
800
801/**
802 * Send a byte (keystroke or command response) to the keyboard controller.
803 *
804 * @returns VINF_SUCCESS or VINF_TRY_AGAIN.
805 * @param pThis The PS/2 auxiliary device instance data.
806 * @param pb Where to return the byte we've read.
807 * @remarks Caller must have entered the device critical section.
808 */
809int PS2MByteFromAux(PPS2M pThis, uint8_t *pb)
810{
811 int rc;
812
813 AssertPtr(pb);
814
815 /* Anything in the command queue has priority over data
816 * in the event queue. Additionally, keystrokes are /// @todo true?
817 * blocked if a command is currently in progress, even if
818 * the command queue is empty.
819 */
820 /// @todo Probably should flush/not fill queue if stream mode reporting disabled?!
821 rc = ps2kRemoveQueue((GeneriQ *)&pThis->cmdQ, pb);
822 if (rc != VINF_SUCCESS && !pThis->u8CurrCmd && (pThis->u8State & AUX_STATE_ENABLED))
823 rc = ps2kRemoveQueue((GeneriQ *)&pThis->evtQ, pb);
824
825 LogFlowFunc(("mouse sends 0x%02x (%svalid data)\n", *pb, rc == VINF_SUCCESS ? "" : "not "));
826//if (rc == VINF_SUCCESS) LogRel(("aux: sends 0x%02X\n", *pb));
827
828 return rc;
829}
830
831#ifdef IN_RING3
832
833/* Event rate throttling timer to emulate the auxiliary device sampling rate.
834 */
835static DECLCALLBACK(void) ps2mThrottleTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
836{
837 RT_NOREF2(pDevIns, pTimer);
838 PPS2M pThis = (PS2M *)pvUser;
839 uint32_t uHaveEvents;
840
841 /* Grab the lock to avoid races with PutEvent(). */
842 int rc = PDMCritSectEnter(pThis->pCritSectR3, VERR_SEM_BUSY);
843 AssertReleaseRC(rc);
844
845#if 0
846 /* If the input queue is not empty, restart the timer. */
847#else
848 /* If more movement is accumulated, report it and restart the timer. */
849 uHaveEvents = pThis->iAccumX | pThis->iAccumY | pThis->iAccumZ | (pThis->fCurrB != pThis->fReportedB);
850 LogFlowFunc(("Have%s events\n", uHaveEvents ? "" : " no"));
851
852 if (uHaveEvents)
853#endif
854 {
855 /* Report accumulated data, poke the KBC, and start the timer. */
856 ps2mReportAccumulatedEvents(pThis, (GeneriQ *)&pThis->evtQ, true);
857 KBCUpdateInterrupts(pThis->pParent);
858 TMTimerSetMillies(pThis->CTX_SUFF(pThrottleTimer), pThis->uThrottleDelay);
859 }
860 else
861 pThis->fThrottleActive = false;
862
863 PDMCritSectLeave(pThis->pCritSectR3);
864}
865
866/* The auxiliary device is specified to take up to about 500 milliseconds. We need
867 * to delay sending the result to the host for at least a tiny little while.
868 */
869static DECLCALLBACK(void) ps2mDelayTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
870{
871 RT_NOREF2(pDevIns, pTimer);
872 PPS2M pThis = (PS2M *)pvUser;
873
874 LogFlowFunc(("Delay timer: cmd %02X\n", pThis->u8CurrCmd));
875
876 Assert(pThis->u8CurrCmd == ACMD_RESET);
877 ps2mReset(pThis);
878
879 /// @todo Might want a PS2MCompleteCommand() to push last response, clear command, and kick the KBC...
880 /* Give the KBC a kick. */
881 KBCUpdateInterrupts(pThis->pParent);
882}
883
884
885/**
886 * Debug device info handler. Prints basic auxiliary device state.
887 *
888 * @param pDevIns Device instance which registered the info.
889 * @param pHlp Callback functions for doing output.
890 * @param pszArgs Argument string. Optional and specific to the handler.
891 */
892static DECLCALLBACK(void) ps2mInfoState(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
893{
894 static const char *pcszModes[] = { "normal", "reset", "wrap" };
895 static const char *pcszProtocols[] = { "PS/2", NULL, NULL, "ImPS/2", "ImEx" };
896 PPS2M pThis = KBDGetPS2MFromDevIns(pDevIns);
897 NOREF(pszArgs);
898
899 Assert(pThis->enmMode <= RT_ELEMENTS(pcszModes));
900 Assert(pThis->enmProtocol <= RT_ELEMENTS(pcszProtocols));
901 pHlp->pfnPrintf(pHlp, "PS/2 mouse state: %s, %s mode, reporting %s\n",
902 pcszModes[pThis->enmMode],
903 pThis->u8State & AUX_STATE_REMOTE ? "remote" : "stream",
904 pThis->u8State & AUX_STATE_ENABLED ? "enabled" : "disabled");
905 pHlp->pfnPrintf(pHlp, "Protocol: %s, scaling %u:1\n",
906 pcszProtocols[pThis->enmProtocol], pThis->u8State & AUX_STATE_SCALING ? 2 : 1);
907 pHlp->pfnPrintf(pHlp, "Active command %02X\n", pThis->u8CurrCmd);
908 pHlp->pfnPrintf(pHlp, "Sampling rate %u reports/sec, resolution %u counts/mm\n",
909 pThis->u8SampleRate, 1 << pThis->u8Resolution);
910 pHlp->pfnPrintf(pHlp, "Command queue: %d items (%d max)\n",
911 pThis->cmdQ.cUsed, pThis->cmdQ.cSize);
912 pHlp->pfnPrintf(pHlp, "Event queue : %d items (%d max)\n",
913 pThis->evtQ.cUsed, pThis->evtQ.cSize);
914}
915
916/* -=-=-=-=-=- Mouse: IBase -=-=-=-=-=- */
917
918/**
919 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
920 */
921static DECLCALLBACK(void *) ps2mQueryInterface(PPDMIBASE pInterface, const char *pszIID)
922{
923 PPS2M pThis = RT_FROM_MEMBER(pInterface, PS2M, Mouse.IBase);
924 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Mouse.IBase);
925 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThis->Mouse.IPort);
926 return NULL;
927}
928
929
930/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
931
932/**
933 * Mouse event handler.
934 *
935 * @returns VBox status code.
936 * @param pThis The PS/2 auxiliary device instance data.
937 * @param dx X direction movement delta.
938 * @param dy Y direction movement delta.
939 * @param dz Z (vertical scroll) movement delta.
940 * @param dw W (horizontal scroll) movement delta.
941 * @param fButtons Depressed button mask.
942 */
943static int ps2mPutEventWorker(PPS2M pThis, int32_t dx, int32_t dy,
944 int32_t dz, int32_t dw, uint32_t fButtons)
945{
946 RT_NOREF1(dw);
947 int rc = VINF_SUCCESS;
948
949 /* Update internal accumulators and button state. */
950 pThis->iAccumX += dx;
951 pThis->iAccumY += dy;
952 pThis->iAccumZ += dz;
953 pThis->fAccumB |= fButtons; /// @todo accumulate based on current protocol?
954 pThis->fCurrB = fButtons;
955
956#if 1
957 /* Report the event and start the throttle timer unless it's already running. */
958 if (!pThis->fThrottleActive)
959 {
960 ps2mReportAccumulatedEvents(pThis, (GeneriQ *)&pThis->evtQ, true);
961 KBCUpdateInterrupts(pThis->pParent);
962 pThis->fThrottleActive = true;
963 TMTimerSetMillies(pThis->CTX_SUFF(pThrottleTimer), pThis->uThrottleDelay);
964 }
965#else
966 /* Clamp the delta values to the allowed range. */
967 dx = RT_MIN(RT_MAX(dx, -256), 255);
968 dy = RT_MIN(RT_MAX(dy, -256), 255);
969
970 /* Start with the sync bit. */
971 val = RT_BIT(3);
972 /* Add buttons 1-3. */
973 val |= fButtons & PS2M_STD_BTN_MASK;
974 /* Set the X/Y sign bits. */
975 if (dx < 0)
976 val |= RT_BIT(4);
977 if (dy < 0)
978 val |= RT_BIT(5);
979
980 ps2kInsertQueue((GeneriQ *)&pThis->evtQ, val);
981 ps2kInsertQueue((GeneriQ *)&pThis->evtQ, (uint8_t)dx);
982 ps2kInsertQueue((GeneriQ *)&pThis->evtQ, (uint8_t)dy);
983 if (pThis->enmProtocol > PS2M_PROTO_PS2STD)
984 {
985 ps2kInsertQueue((GeneriQ *)&pThis->evtQ, (uint8_t)dz);
986 }
987#endif
988
989 return rc;
990}
991
992/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
993
994/**
995 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEvent}
996 */
997static DECLCALLBACK(int) ps2mPutEvent(PPDMIMOUSEPORT pInterface, int32_t dx, int32_t dy,
998 int32_t dz, int32_t dw, uint32_t fButtons)
999{
1000 PPS2M pThis = RT_FROM_MEMBER(pInterface, PS2M, Mouse.IPort);
1001 int rc = PDMCritSectEnter(pThis->pCritSectR3, VERR_SEM_BUSY);
1002 AssertReleaseRC(rc);
1003
1004 LogRelFlowFunc(("dX=%d dY=%d dZ=%d dW=%d buttons=%02X\n", dx, dy, dz, dw, fButtons));
1005 /* NB: The PS/2 Y axis direction is inverted relative to ours. */
1006 ps2mPutEventWorker(pThis, dx, -dy, dz, dw, fButtons);
1007
1008 PDMCritSectLeave(pThis->pCritSectR3);
1009 return VINF_SUCCESS;
1010}
1011
1012/**
1013 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEventAbs}
1014 */
1015static DECLCALLBACK(int) ps2mPutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t x, uint32_t y,
1016 int32_t dz, int32_t dw, uint32_t fButtons)
1017{
1018 AssertFailedReturn(VERR_NOT_SUPPORTED);
1019 NOREF(pInterface); NOREF(x); NOREF(y); NOREF(dz); NOREF(dw); NOREF(fButtons);
1020}
1021
1022/**
1023 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEventMultiTouch}
1024 */
1025static DECLCALLBACK(int) ps2mPutEventMT(PPDMIMOUSEPORT pInterface, uint8_t cContacts,
1026 const uint64_t *pau64Contacts, uint32_t u32ScanTime)
1027{
1028 AssertFailedReturn(VERR_NOT_SUPPORTED);
1029 NOREF(pInterface); NOREF(cContacts); NOREF(pau64Contacts); NOREF(u32ScanTime);
1030}
1031
1032
1033
1034/**
1035 * Attach command.
1036 *
1037 * This is called to let the device attach to a driver for a
1038 * specified LUN.
1039 *
1040 * This is like plugging in the mouse after turning on the
1041 * system.
1042 *
1043 * @returns VBox status code.
1044 * @param pThis The PS/2 auxiliary device instance data.
1045 * @param pDevIns The device instance.
1046 * @param iLUN The logical unit which is being detached.
1047 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1048 */
1049int PS2MAttach(PPS2M pThis, PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
1050{
1051 int rc;
1052
1053 /* The LUN must be 1, i.e. mouse. */
1054 Assert(iLUN == 1);
1055 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
1056 ("PS/2 mouse does not support hotplugging\n"),
1057 VERR_INVALID_PARAMETER);
1058
1059 LogFlowFunc(("iLUN=%d\n", iLUN));
1060
1061 rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThis->Mouse.IBase, &pThis->Mouse.pDrvBase, "Mouse Port");
1062 if (RT_SUCCESS(rc))
1063 {
1064 pThis->Mouse.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Mouse.pDrvBase, PDMIMOUSECONNECTOR);
1065 if (!pThis->Mouse.pDrv)
1066 {
1067 AssertLogRelMsgFailed(("LUN #1 doesn't have a mouse interface! rc=%Rrc\n", rc));
1068 rc = VERR_PDM_MISSING_INTERFACE;
1069 }
1070 }
1071 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
1072 {
1073 Log(("%s/%d: warning: no driver attached to LUN #1!\n", pDevIns->pReg->szName, pDevIns->iInstance));
1074 rc = VINF_SUCCESS;
1075 }
1076 else
1077 AssertLogRelMsgFailed(("Failed to attach LUN #1! rc=%Rrc\n", rc));
1078
1079 return rc;
1080}
1081
1082void PS2MSaveState(PPS2M pThis, PSSMHANDLE pSSM)
1083{
1084 LogFlowFunc(("Saving PS2M state\n"));
1085
1086 /* Save the core auxiliary device state. */
1087 SSMR3PutU8(pSSM, pThis->u8State);
1088 SSMR3PutU8(pSSM, pThis->u8SampleRate);
1089 SSMR3PutU8(pSSM, pThis->u8Resolution);
1090 SSMR3PutU8(pSSM, pThis->u8CurrCmd);
1091 SSMR3PutU8(pSSM, pThis->enmMode);
1092 SSMR3PutU8(pSSM, pThis->enmProtocol);
1093 SSMR3PutU8(pSSM, pThis->enmKnockState);
1094
1095 /* Save the command and event queues. */
1096 ps2kSaveQueue(pSSM, (GeneriQ *)&pThis->cmdQ);
1097 ps2kSaveQueue(pSSM, (GeneriQ *)&pThis->evtQ);
1098
1099 /* Save the command delay timer. Note that the rate throttling
1100 * timer is *not* saved.
1101 */
1102 TMR3TimerSave(pThis->CTX_SUFF(pDelayTimer), pSSM);
1103}
1104
1105int PS2MLoadState(PPS2M pThis, PSSMHANDLE pSSM, uint32_t uVersion)
1106{
1107 uint8_t u8;
1108 int rc;
1109
1110 NOREF(uVersion);
1111 LogFlowFunc(("Loading PS2M state version %u\n", uVersion));
1112
1113 /* Load the basic auxiliary device state. */
1114 SSMR3GetU8(pSSM, &pThis->u8State);
1115 SSMR3GetU8(pSSM, &pThis->u8SampleRate);
1116 SSMR3GetU8(pSSM, &pThis->u8Resolution);
1117 SSMR3GetU8(pSSM, &pThis->u8CurrCmd);
1118 SSMR3GetU8(pSSM, &u8);
1119 pThis->enmMode = (PS2M_MODE)u8;
1120 SSMR3GetU8(pSSM, &u8);
1121 pThis->enmProtocol = (PS2M_PROTO)u8;
1122 SSMR3GetU8(pSSM, &u8);
1123 pThis->enmKnockState = (PS2M_KNOCK_STATE)u8;
1124
1125 /* Load the command and event queues. */
1126 rc = ps2kLoadQueue(pSSM, (GeneriQ *)&pThis->cmdQ);
1127 AssertRCReturn(rc, rc);
1128 rc = ps2kLoadQueue(pSSM, (GeneriQ *)&pThis->evtQ);
1129 AssertRCReturn(rc, rc);
1130
1131 /* Load the command delay timer, just in case. */
1132 rc = TMR3TimerLoad(pThis->CTX_SUFF(pDelayTimer), pSSM);
1133 AssertRCReturn(rc, rc);
1134
1135 /* Recalculate the throttling delay. */
1136 ps2mSetRate(pThis, pThis->u8SampleRate);
1137
1138 ps2mSetDriverState(pThis, !!(pThis->u8State & AUX_STATE_ENABLED));
1139
1140 return rc;
1141}
1142
1143void PS2MFixupState(PPS2M pThis, uint8_t u8State, uint8_t u8Rate, uint8_t u8Proto)
1144{
1145 LogFlowFunc(("Fixing up old PS2M state version\n"));
1146
1147 /* Load the basic auxiliary device state. */
1148 pThis->u8State = u8State;
1149 pThis->u8SampleRate = u8Rate ? u8Rate : 40; /* In case it wasn't saved right. */
1150 pThis->enmProtocol = (PS2M_PROTO)u8Proto;
1151
1152 /* Recalculate the throttling delay. */
1153 ps2mSetRate(pThis, pThis->u8SampleRate);
1154
1155 ps2mSetDriverState(pThis, !!(pThis->u8State & AUX_STATE_ENABLED));
1156}
1157
1158void PS2MReset(PPS2M pThis)
1159{
1160 LogFlowFunc(("Resetting PS2M\n"));
1161
1162 pThis->u8CurrCmd = 0;
1163
1164 /* Clear the queues. */
1165 ps2kClearQueue((GeneriQ *)&pThis->cmdQ);
1166 ps2mSetDefaults(pThis); /* Also clears event queue. */
1167
1168 /* Activate the PS/2 mouse by default. */
1169// if (pThis->Mouse.pDrv)
1170// pThis->Mouse.pDrv->pfnSetActive(pThis->Mouse.pDrv, true);
1171}
1172
1173void PS2MRelocate(PPS2M pThis, RTGCINTPTR offDelta, PPDMDEVINS pDevIns)
1174{
1175 RT_NOREF2(pDevIns, offDelta);
1176 LogFlowFunc(("Relocating PS2M\n"));
1177 pThis->pDelayTimerRC = TMTimerRCPtr(pThis->pDelayTimerR3);
1178 pThis->pThrottleTimerRC = TMTimerRCPtr(pThis->pThrottleTimerR3);
1179}
1180
1181int PS2MConstruct(PPS2M pThis, PPDMDEVINS pDevIns, void *pParent, int iInstance)
1182{
1183 RT_NOREF1(iInstance);
1184
1185 LogFlowFunc(("iInstance=%d\n", iInstance));
1186
1187 pThis->pParent = pParent;
1188
1189 /* Initialize the queues. */
1190 pThis->evtQ.cSize = AUX_EVT_QUEUE_SIZE;
1191 pThis->cmdQ.cSize = AUX_CMD_QUEUE_SIZE;
1192
1193 pThis->Mouse.IBase.pfnQueryInterface = ps2mQueryInterface;
1194 pThis->Mouse.IPort.pfnPutEvent = ps2mPutEvent;
1195 pThis->Mouse.IPort.pfnPutEventAbs = ps2mPutEventAbs;
1196 pThis->Mouse.IPort.pfnPutEventMultiTouch = ps2mPutEventMT;
1197
1198 /*
1199 * Initialize the critical section pointer(s).
1200 */
1201 pThis->pCritSectR3 = pDevIns->pCritSectRoR3;
1202
1203 /*
1204 * Create the input rate throttling timer. Does not use virtual time!
1205 */
1206 PTMTIMER pTimer;
1207 int rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_REAL, ps2mThrottleTimer, pThis,
1208 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2M Throttle Timer", &pTimer);
1209 if (RT_FAILURE(rc))
1210 return rc;
1211
1212 pThis->pThrottleTimerR3 = pTimer;
1213 pThis->pThrottleTimerR0 = TMTimerR0Ptr(pTimer);
1214 pThis->pThrottleTimerRC = TMTimerRCPtr(pTimer);
1215
1216 /*
1217 * Create the command delay timer.
1218 */
1219 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ps2mDelayTimer, pThis,
1220 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2M Delay Timer", &pTimer);
1221 if (RT_FAILURE(rc))
1222 return rc;
1223
1224 pThis->pDelayTimerR3 = pTimer;
1225 pThis->pDelayTimerR0 = TMTimerR0Ptr(pTimer);
1226 pThis->pDelayTimerRC = TMTimerRCPtr(pTimer);
1227
1228 /*
1229 * Register debugger info callbacks.
1230 */
1231 PDMDevHlpDBGFInfoRegister(pDevIns, "ps2m", "Display PS/2 mouse state.", ps2mInfoState);
1232
1233 /// @todo Where should we do this?
1234 ps2mSetDriverState(pThis, true);
1235 pThis->u8State = 0;
1236 pThis->enmMode = AUX_MODE_STD;
1237
1238 return rc;
1239}
1240
1241#endif
1242
1243#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