VirtualBox

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

Last change on this file since 56292 was 56292, checked in by vboxsync, 10 years ago

Devices: Updated (C) year.

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