PS2M.cpp revision 892ba2c161e62c8e858f2fcf299c38951e330d39
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/* $Id$ */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/** @file
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * PS2M - PS/2 auxiliary device (mouse) emulation.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
e218a78cf91e0a6e295bffe1998e96b308c5a13avboxsync/*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Copyright (C) 2007-2013 Oracle Corporation
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * available from http://www.virtualbox.org. This file is free software;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * you can redistribute it and/or modify it under the terms of the GNU
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * General Public License (GPL) as published by the Free Software
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * References:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * The Undocumented PC (2nd Ed.), Frank van Gilluwe, Addison-Wesley, 1996.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * IBM TrackPoint System Version 4.0 Engineering Specification, 1999.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * ELAN Microelectronics eKM8025 USB & PS/2 Mouse Controller, 2006.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Notes:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * - The auxiliary device commands are very similar to keyboard commands.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Most keyboard commands which do not specifically deal with the keyboard
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * (enable, disable, reset) have identical counterparts.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * - The code refers to 'auxiliary device' and 'mouse'; these terms are not
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * quite interchangeable. 'Auxiliary device' is used when referring to the
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * generic PS/2 auxiliary device interface and 'mouse' when referring to
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * a mouse attached to the auxiliary port.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * - The basic modes of operation are reset, stream, and remote. Those are
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * mutually exclusive. Stream and remote modes can additionally have wrap
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * mode enabled.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * - The auxiliary device sends unsolicited data to the host only when it is
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * both in stream mode and enabled. Otherwise it only responds to commands.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * There are three report packet formats supported by the emulated device. The
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * standard three-byte PS/2 format (with middle button support), IntelliMouse
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * four-byte format with added scroll wheel, and IntelliMouse Explorer four-byte
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * format with reduced scroll wheel range but two additional buttons. Note that
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * the first three bytes of the report are always the same.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Upon reset, the mouse is always in the standard PS/2 mode. A special 'knock'
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * sequence can be used to switch to ImPS/2 or ImEx mode. Three consecutive
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Set Sampling Rate (0F3h) commands with arguments 200, 100, 80 switch to ImPS/2
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * mode. While in ImPS/2 or PS/2 mode, three consecutive Set Sampling Rate
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * commands with arguments 200, 200, 80 switch to ImEx mode. The Read ID (0F2h)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * command will report the currently selected protocol.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Standard PS/2 pointing device three-byte report packet format:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * |Bit/byte| bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * | Byte 1 | Y ovfl | X ovfl | Y sign | X sign | Sync | M btn | R btn | L btn |
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * | Byte 2 | X movement delta (two's complement) |
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * | Byte 3 | Y movement delta (two's complement) |
ae20b83f0c94402a3e3ac021c3d4e5f827e4905cvboxsync * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
ae20b83f0c94402a3e3ac021c3d4e5f827e4905cvboxsync *
ae20b83f0c94402a3e3ac021c3d4e5f827e4905cvboxsync * - The sync bit is always set. It allows software to synchronize data packets
ae20b83f0c94402a3e3ac021c3d4e5f827e4905cvboxsync * as the X/Y position data typically does not have bit 4 set.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * - The overflow bits are set if motion exceeds accumulator range. We use the
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * maximum range (effectively 9 bits) and do not set the overflow bits.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * - Movement in the up/right direction is defined as having positive sign.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * IntelliMouse PS/2 (ImPS/2) fourth report packet byte:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * |Bit/byte| bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * | Byte 4 | Z movement delta (two's complement) |
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * - The valid range for Z delta values is only -8/+7, i.e. 4 bits.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * IntelliMouse Explorer (ImEx) fourth report packet byte:
afa761a969c8883e5ea370e898d40ce053fbcb22vboxsync *
afa761a969c8883e5ea370e898d40ce053fbcb22vboxsync * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
8c7f1eb426ed2259c4ed882d80af2666b2490867vboxsync * |Bit/byte| bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
8c7f1eb426ed2259c4ed882d80af2666b2490867vboxsync * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
8c7f1eb426ed2259c4ed882d80af2666b2490867vboxsync * | Byte 4 | 0 | 0 | Btn 5 | Btn 4 | Z mov't delta (two's complement) |
8c7f1eb426ed2259c4ed882d80af2666b2490867vboxsync * +--------+--------+--------+--------+--------+--------+--------+--------+--------+
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/*******************************************************************************
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync* Header Files *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync*******************************************************************************/
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define LOG_GROUP LOG_GROUP_DEV_KBD
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/vmm/pdmdev.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/err.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <iprt/assert.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <iprt/uuid.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include "VBoxDD.h"
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define IN_PS2M
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include "PS2Dev.h"
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/*******************************************************************************
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync* Defined Constants And Macros *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync*******************************************************************************/
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/** @name Auxiliary device commands sent by the system.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @{ */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_SET_SCALE_11 0xE6 /* Set 1:1 scaling. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_SET_SCALE_21 0xE7 /* Set 2:1 scaling. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_SET_RES 0xE8 /* Set resolution. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_REQ_STATUS 0xE9 /* Get device status. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_SET_STREAM 0xEA /* Set stream mode. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_READ_REMOTE 0xEB /* Read remote data. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_RESET_WRAP 0xEC /* Exit wrap mode. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_INVALID_1 0xED
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_SET_WRAP 0xEE /* Set wrap (echo) mode. */
c618f4d0eeea6061ff8270e8f7023a999bd0bb91vboxsync#define ACMD_INVALID_2 0xEF
c618f4d0eeea6061ff8270e8f7023a999bd0bb91vboxsync#define ACMD_SET_REMOTE 0xF0 /* Set remote mode. */
c618f4d0eeea6061ff8270e8f7023a999bd0bb91vboxsync#define ACMD_INVALID_3 0xF1
c618f4d0eeea6061ff8270e8f7023a999bd0bb91vboxsync#define ACMD_READ_ID 0xF2 /* Read device ID. */
c618f4d0eeea6061ff8270e8f7023a999bd0bb91vboxsync#define ACMD_SET_SAMP_RATE 0xF3 /* Set sampling rate. */
c618f4d0eeea6061ff8270e8f7023a999bd0bb91vboxsync#define ACMD_ENABLE 0xF4 /* Enable (streaming mode). */
c618f4d0eeea6061ff8270e8f7023a999bd0bb91vboxsync#define ACMD_DISABLE 0xF5 /* Disable (streaming mode). */
c618f4d0eeea6061ff8270e8f7023a999bd0bb91vboxsync#define ACMD_SET_DEFAULT 0xF6 /* Set defaults. */
c618f4d0eeea6061ff8270e8f7023a999bd0bb91vboxsync#define ACMD_INVALID_4 0xF7
c618f4d0eeea6061ff8270e8f7023a999bd0bb91vboxsync#define ACMD_INVALID_5 0xF8
c618f4d0eeea6061ff8270e8f7023a999bd0bb91vboxsync#define ACMD_INVALID_6 0xF9
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_INVALID_7 0xFA
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_INVALID_8 0xFB
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_INVALID_9 0xFC
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_INVALID_10 0xFD
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_RESEND 0xFE /* Resend response. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ACMD_RESET 0xFF /* Reset device. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/** @} */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/** @name Auxiliary device responses sent to the system.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @{ */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ARSP_ID 0x00
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ARSP_BAT_OK 0xAA /* Self-test passed. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ARSP_ACK 0xFA /* Command acknowledged. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ARSP_ERROR 0xFC /* Bad command. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define ARSP_RESEND 0xFE /* Requesting resend. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/** @} */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/** Define a simple PS/2 input device queue. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define DEF_PS2Q_TYPE(name, size) \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync typedef struct { \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uint32_t rpos; \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uint32_t wpos; \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uint32_t cUsed; \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uint32_t cSize; \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uint8_t abQueue[size]; \
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync } name
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/* Internal mouse queue sizes. The input queue is relatively large,
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * but the command queue only needs to handle a few bytes.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync#define AUX_EVT_QUEUE_SIZE 256
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define AUX_CMD_QUEUE_SIZE 8
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/*******************************************************************************
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync* Structures and Typedefs *
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync*******************************************************************************/
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncDEF_PS2Q_TYPE(AuxEvtQ, AUX_EVT_QUEUE_SIZE);
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsyncDEF_PS2Q_TYPE(AuxCmdQ, AUX_CMD_QUEUE_SIZE);
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync#ifndef VBOX_DEVICE_STRUCT_TESTCASE //@todo: hack
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsyncDEF_PS2Q_TYPE(GeneriQ, 1);
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync#endif
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync/* Auxiliary device special modes of operation. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsynctypedef enum {
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync AUX_MODE_STD, /* Standard operation. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync AUX_MODE_RESET, /* Currently in reset. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync AUX_MODE_WRAP /* Wrap mode (echoing input). */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync} PS2M_MODE;
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync/* Auxiliary device operational state. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsynctypedef enum {
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync AUX_STATE_SCALING = RT_BIT(4), /* 2:1 scaling in effect. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync AUX_STATE_ENABLED = RT_BIT(5), /* Reporting enabled in stream mode. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync AUX_STATE_REMOTE = RT_BIT(6) /* Remote mode (reports on request). */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync} PS2M_STATE;
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync/* Protocols supported by the PS/2 mouse. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsynctypedef enum {
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync PS2M_PROTO_PS2STD = 0, /* Standard PS/2 mouse protocol. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync PS2M_PROTO_IMPS2 = 3, /* IntelliMouse PS/2 protocol. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync PS2M_PROTO_IMEX = 4 /* IntelliMouse Explorer protocol. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync} PS2M_PROTO;
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync/* Protocol selection 'knock' states. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsynctypedef enum {
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync PS2M_KNOCK_INITIAL,
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync PS2M_KNOCK_1ST,
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync PS2M_KNOCK_IMPS2_2ND,
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync PS2M_KNOCK_IMEX_2ND
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync} PS2M_KNOCK_STATE;
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync/**
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync * The PS/2 auxiliary device instance data.
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsynctypedef struct PS2M
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync{
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync /** Pointer to parent device (keyboard controller). */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync R3PTRTYPE(void *) pParent;
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync /** Operational state. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync uint8_t u8State;
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync /** Configured sampling rate. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync uint8_t u8SampleRate;
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync /** Configured resolution. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync uint8_t u8Resolution;
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync /** Currently processed command (if any). */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync uint8_t u8CurrCmd;
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync /** Set if the throttle delay is active. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync bool fThrottleActive;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** Set if the throttle delay is active. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync bool fDelayReset;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** Operational mode. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PS2M_MODE enmMode;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** Currently used protocol. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PS2M_PROTO enmProtocol;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** Currently used protocol. */
486d3fda5cadcf6d8357df872455cdd8796b3556vboxsync PS2M_KNOCK_STATE enmKnockState;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** Buffer holding mouse events to be sent to the host. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AuxEvtQ evtQ;
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync /** Command response queue (priority). */
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync AuxCmdQ cmdQ;
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync /** Accumulated horizontal movement. */
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync int32_t iAccumX;
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync /** Accumulated vertical movement. */
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync int32_t iAccumY;
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync /** Accumulated Z axis movement. */
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync int32_t iAccumZ;
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync /** Accumulated button presses. */
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync uint32_t fAccumB;
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync /** Instantaneous button data. */
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync uint32_t fCurrB;
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync /** Throttling delay in milliseconds. */
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync uint32_t uThrottleDelay;
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync uint32_t Alignment0;
e218a78cf91e0a6e295bffe1998e96b308c5a13avboxsync
e218a78cf91e0a6e295bffe1998e96b308c5a13avboxsync /** The device critical section protecting everything - R3 Ptr */
e218a78cf91e0a6e295bffe1998e96b308c5a13avboxsync R3PTRTYPE(PPDMCRITSECT) pCritSectR3;
e218a78cf91e0a6e295bffe1998e96b308c5a13avboxsync /** Command delay timer - R3 Ptr. */
e218a78cf91e0a6e295bffe1998e96b308c5a13avboxsync PTMTIMERR3 pDelayTimerR3;
e218a78cf91e0a6e295bffe1998e96b308c5a13avboxsync /** Interrupt throttling timer - R3 Ptr. */
e218a78cf91e0a6e295bffe1998e96b308c5a13avboxsync PTMTIMERR3 pThrottleTimerR3;
e218a78cf91e0a6e295bffe1998e96b308c5a13avboxsync RTR3PTR Alignment1;
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync
e218a78cf91e0a6e295bffe1998e96b308c5a13avboxsync /** Command delay timer - RC Ptr. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PTMTIMERRC pDelayTimerRC;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** Interrupt throttling timer - RC Ptr. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PTMTIMERRC pThrottleTimerRC;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** Command delay timer - R0 Ptr. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PTMTIMERR0 pDelayTimerR0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** Interrupt throttling timer - R0 Ptr. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PTMTIMERR0 pThrottleTimerR0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
a000f6190af85455eefaa1097deaeea087cfc5e5vboxsync /**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Mouse port - LUN#1.
a000f6190af85455eefaa1097deaeea087cfc5e5vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @implements PDMIBASE
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @implements PDMIMOUSEPORT
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync struct
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** The base interface for the mouse port. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PDMIBASE IBase;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** The keyboard port base interface. */
1b28537ff3aabbe3afa3fa15cc0436384ff1c827vboxsync PDMIMOUSEPORT IPort;
a000f6190af85455eefaa1097deaeea087cfc5e5vboxsync
a000f6190af85455eefaa1097deaeea087cfc5e5vboxsync /** The base interface of the attached mouse driver. */
a000f6190af85455eefaa1097deaeea087cfc5e5vboxsync R3PTRTYPE(PPDMIBASE) pDrvBase;
a000f6190af85455eefaa1097deaeea087cfc5e5vboxsync /** The keyboard interface of the attached mouse driver. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync R3PTRTYPE(PPDMIMOUSECONNECTOR) pDrv;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync } Mouse;
a000f6190af85455eefaa1097deaeea087cfc5e5vboxsync} PS2M, *PPS2M;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncAssertCompile(PS2M_STRUCT_FILLER >= sizeof(PS2M));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifndef VBOX_DEVICE_STRUCT_TESTCASE
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/*******************************************************************************
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync* Global Variables *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync*******************************************************************************/
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
90bb13bdf70379f91f6aeb334f70287a5096fb02vboxsync/*******************************************************************************
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync* Internal Functions *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync*******************************************************************************/
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Clear a queue.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pQ Pointer to the queue.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic void ps2kClearQueue(GeneriQ *pQ)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogFlowFunc(("Clearing queue %p\n", pQ));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pQ->wpos = pQ->rpos;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pQ->cUsed = 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Add a byte to a queue.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pQ Pointer to the queue.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param val The byte to store.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic void ps2kInsertQueue(GeneriQ *pQ, uint8_t val)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Check if queue is full. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pQ->cUsed >= pQ->cSize)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogFlowFunc(("queue %p full (%d entries)\n", pQ, pQ->cUsed));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Insert data and update circular buffer write position. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pQ->abQueue[pQ->wpos] = val;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (++pQ->wpos == pQ->cSize)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pQ->wpos = 0; /* Roll over. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ++pQ->cUsed;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogFlowFunc(("inserted 0x%02X into queue %p\n", val, pQ));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef IN_RING3
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
d7fe26caad92e0d13017738ab94de18e37be91b4vboxsync * Save a queue state.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pSSM SSM handle to write the state to.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pQ Pointer to the queue.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic void ps2kSaveQueue(PSSMHANDLE pSSM, GeneriQ *pQ)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uint32_t cItems = pQ->cUsed;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int i;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Only save the number of items. Note that the read/write
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * positions aren't saved as they will be rebuilt on load.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync SSMR3PutU32(pSSM, cItems);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogFlow(("Storing %d items from queue %p\n", cItems, pQ));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Save queue data - only the bytes actually used (typically zero). */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync for (i = pQ->rpos; cItems-- > 0; i = (i + 1) % pQ->cSize)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync SSMR3PutU8(pSSM, pQ->abQueue[i]);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Load a queue state.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pSSM SSM handle to read the state from.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pQ Pointer to the queue.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @return int VBox status/error code.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic int ps2kLoadQueue(PSSMHANDLE pSSM, GeneriQ *pQ)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
d7fe26caad92e0d13017738ab94de18e37be91b4vboxsync int rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
d7fe26caad92e0d13017738ab94de18e37be91b4vboxsync /* On load, always put the read pointer at zero. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync SSMR3GetU32(pSSM, &pQ->cUsed);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogFlow(("Loading %d items to queue %p\n", pQ->cUsed, pQ));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pQ->cUsed > pQ->cSize)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync AssertMsgFailed(("Saved size=%u, actual=%u\n", pQ->cUsed, pQ->cSize));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
4f007e159d64d64366398032617896c3a982fa42vboxsync
4f007e159d64d64366398032617896c3a982fa42vboxsync /* Recalculate queue positions and load data in one go. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pQ->rpos = 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pQ->wpos = pQ->cUsed;
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync rc = SSMR3GetMem(pSSM, pQ->abQueue, pQ->cUsed);
e2bd93b4f9c38c9b01eb960ba7bc1fc9c4d38ce8vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/* Report a change in status down (or is it up?) the driver chain. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic void ps2mSetDriverState(PPS2M pThis, bool fEnabled)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPDMIMOUSECONNECTOR pDrv = pThis->Mouse.pDrv;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pDrv)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pDrv->pfnReportModes(pDrv, fEnabled, false, false);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/* Reset the pointing device. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic void ps2mReset(PPS2M pThis)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_BAT_OK);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, 0);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->enmMode = AUX_MODE_STD;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->u8CurrCmd = 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync //@todo: move to its proper home!
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ps2mSetDriverState(pThis, true);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif /* IN_RING3 */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Retrieve a byte from a queue.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pQ Pointer to the queue.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param pVal Pointer to storage for the byte.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
4d4f336b656d46f8d301603114bb99ce635aafc0vboxsync * @return int VINF_TRY_AGAIN if queue is empty,
4d4f336b656d46f8d301603114bb99ce635aafc0vboxsync * VINF_SUCCESS if a byte was read.
4d4f336b656d46f8d301603114bb99ce635aafc0vboxsync */
4d4f336b656d46f8d301603114bb99ce635aafc0vboxsyncstatic int ps2kRemoveQueue(GeneriQ *pQ, uint8_t *pVal)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rc = VINF_TRY_AGAIN;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(pVal);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pQ->cUsed)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *pVal = pQ->abQueue[pQ->rpos];
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (++pQ->rpos == pQ->cSize)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pQ->rpos = 0; /* Roll over. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync --pQ->cUsed;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rc = VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogFlowFunc(("removed 0x%02X from queue %p\n", *pVal, pQ));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync } else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogFlowFunc(("queue %p empty\n", pQ));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return rc;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic void ps2mSetRate(PPS2M pThis, uint8_t rate)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->uThrottleDelay = 1000 / rate;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->u8SampleRate = rate;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogFlowFunc(("Sampling rate %u, throttle delay %u ms\n", pThis->u8SampleRate, pThis->uThrottleDelay));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic void ps2mSetDefaults(PPS2M pThis)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogFlowFunc(("Set mouse defaults\n"));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Standard protocol, reporting disabled, resolution 2, 1:1 scaling. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->enmProtocol = PS2M_PROTO_PS2STD;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->u8State = 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->u8Resolution = 2;
0547b5d21ca43e39c3b6fba8cd9988b1338d61d0vboxsync
0547b5d21ca43e39c3b6fba8cd9988b1338d61d0vboxsync /* Sample rate 100 reports per second. */
0547b5d21ca43e39c3b6fba8cd9988b1338d61d0vboxsync ps2mSetRate(pThis, 100);
0547b5d21ca43e39c3b6fba8cd9988b1338d61d0vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Event queue, eccumulators, and button status bits are cleared. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ps2kClearQueue((GeneriQ *)&pThis->evtQ);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->iAccumX = pThis->iAccumY = pThis->iAccumZ = pThis->fAccumB = pThis->fCurrB = 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/* Handle the sampling rate 'knock' sequence which selects protocol. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic void ps2mRateProtocolKnock(PPS2M pThis, uint8_t rate)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync switch (pThis->enmKnockState)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync case PS2M_KNOCK_INITIAL:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (rate == 200)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->enmKnockState = PS2M_KNOCK_1ST;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync case PS2M_KNOCK_1ST:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (rate == 100)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->enmKnockState = PS2M_KNOCK_IMPS2_2ND;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else if (rate == 200)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->enmKnockState = PS2M_KNOCK_IMEX_2ND;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->enmKnockState = PS2M_KNOCK_INITIAL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync case PS2M_KNOCK_IMPS2_2ND:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (rate == 80)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->enmProtocol = PS2M_PROTO_IMPS2;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogRelFlow(("PS2M: Switching mouse to ImPS/2 protocol.\n"));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->enmKnockState = PS2M_KNOCK_INITIAL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync break;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync case PS2M_KNOCK_IMEX_2ND:
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (rate == 80)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->enmProtocol = PS2M_PROTO_IMEX;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync LogRelFlow(("PS2M: Switching mouse to ImEx protocol.\n"));
9577536266a8da7dbbe29fd3be840cbfc0dc9721vboxsync }
9577536266a8da7dbbe29fd3be840cbfc0dc9721vboxsync /* Fall through! */
9577536266a8da7dbbe29fd3be840cbfc0dc9721vboxsync default:
9577536266a8da7dbbe29fd3be840cbfc0dc9721vboxsync pThis->enmKnockState = PS2M_KNOCK_INITIAL;
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync }
9577536266a8da7dbbe29fd3be840cbfc0dc9721vboxsync}
9577536266a8da7dbbe29fd3be840cbfc0dc9721vboxsync
ead20681ed6c34b0fc835c3e6c19b8034856653avboxsync/* Three-button event mask. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#define PS2M_STD_BTN_MASK (RT_BIT(0) | RT_BIT(1) | RT_BIT(2))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/* Report accumulated movement and button presses, then clear the accumulators. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncstatic void ps2mReportAccumulatedEvents(PPS2M pThis, GeneriQ *pQueue, bool fAccumBtns)
969167e7c3b9ef30745755d53585114173e10789vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uint32_t fBtnState = fAccumBtns ? pThis->fAccumB : pThis->fCurrB;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync uint8_t val;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int dX, dY, dZ;
969167e7c3b9ef30745755d53585114173e10789vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Clamp the accumulated delta values to the allowed range. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync dX = RT_MIN(RT_MAX(pThis->iAccumX, -255), 255);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync dY = RT_MIN(RT_MAX(pThis->iAccumY, -255), 255);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync dZ = RT_MIN(RT_MAX(pThis->iAccumZ, -8), 7);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Start with the sync bit and buttons 1-3. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync val = RT_BIT(3) | (fBtnState & PS2M_STD_BTN_MASK);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Set the X/Y sign bits. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (dX < 0)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync val |= RT_BIT(4);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (dY < 0)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync val |= RT_BIT(5);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Send the standard 3-byte packet (always the same). */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ps2kInsertQueue(pQueue, val);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ps2kInsertQueue(pQueue, dX);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ps2kInsertQueue(pQueue, dY);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Add fourth byte if extended protocol is in use. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pThis->enmProtocol > PS2M_PROTO_PS2STD)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (pThis->enmProtocol == PS2M_PROTO_IMPS2)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ps2kInsertQueue(pQueue, dZ);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Assert(pThis->enmProtocol == PS2M_PROTO_IMEX);
89f5aff9eff63826c9b171cc7fdb9dc1513fa09dvboxsync /* Z value uses 4 bits; buttons 4/5 in bits 4 and 5. */
c618f4d0eeea6061ff8270e8f7023a999bd0bb91vboxsync val = dZ & 0x0f;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync val |= (fBtnState << 1) & (RT_BIT(4) | RT_BIT(5));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ps2kInsertQueue(pQueue, dZ);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Clear the movement accumulators. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync pThis->iAccumX = pThis->iAccumY = pThis->iAccumZ = 0;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /* Clear accumulated button state only when it's being used. */
if (fAccumBtns)
pThis->fAccumB = 0;
}
/**
* Receive and process a byte sent by the keyboard controller.
*
* @param pThis The PS/2 auxiliary device instance data.
* @param cmd The command (or data) byte.
*/
int PS2MByteToAux(PPS2M pThis, uint8_t cmd)
{
uint8_t u8Val;
bool fHandled = true;
LogFlowFunc(("cmd=0x%02X, active cmd=0x%02X\n", cmd, pThis->u8CurrCmd));
//LogRel(("aux: cmd=0x%02X, active cmd=0x%02X\n", cmd, pThis->u8CurrCmd));
if (pThis->enmMode == AUX_MODE_RESET)
/* In reset mode, do not respond at all. */
return VINF_SUCCESS;
/* If there's anything left in the command response queue, trash it. */
ps2kClearQueue((GeneriQ *)&pThis->cmdQ);
if (pThis->enmMode == AUX_MODE_WRAP)
{
/* In wrap mode, bounce most data right back.*/
if (cmd == ACMD_RESET || cmd == ACMD_RESET_WRAP)
; /* Handle as regular commands. */
else
{
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, cmd);
return VINF_SUCCESS;
}
}
#ifndef IN_RING3
/* Reset, Enable, and Set Default commands must be run in R3. */
if (cmd == ACMD_RESET || cmd == ACMD_ENABLE || cmd == ACMD_SET_DEFAULT)
return VINF_IOM_R3_IOPORT_WRITE;
#endif
switch (cmd)
{
case ACMD_SET_SCALE_11:
pThis->u8State &= ~AUX_STATE_SCALING;
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
pThis->u8CurrCmd = 0;
break;
case ACMD_SET_SCALE_21:
pThis->u8State |= AUX_STATE_SCALING;
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
pThis->u8CurrCmd = 0;
break;
case ACMD_REQ_STATUS:
/* Report current status, sample rate, and resolution. */
u8Val = pThis->u8State | (pThis->fCurrB & PS2M_STD_BTN_MASK);
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, u8Val);
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, pThis->u8Resolution);
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, pThis->u8SampleRate);
pThis->u8CurrCmd = 0;
break;
case ACMD_SET_STREAM:
pThis->u8State &= ~AUX_STATE_REMOTE;
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
pThis->u8CurrCmd = 0;
break;
case ACMD_READ_REMOTE:
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
ps2mReportAccumulatedEvents(pThis, (GeneriQ *)&pThis->cmdQ, false);
pThis->u8CurrCmd = 0;
break;
case ACMD_RESET_WRAP:
pThis->enmMode = AUX_MODE_STD;
/* NB: Stream mode reporting remains disabled! */
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
pThis->u8CurrCmd = 0;
break;
case ACMD_SET_WRAP:
pThis->enmMode = AUX_MODE_WRAP;
pThis->u8State &= ~AUX_STATE_ENABLED;
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
pThis->u8CurrCmd = 0;
break;
case ACMD_SET_REMOTE:
pThis->u8State |= AUX_STATE_REMOTE;
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
pThis->u8CurrCmd = 0;
break;
case ACMD_READ_ID:
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, pThis->enmProtocol);
pThis->u8CurrCmd = 0;
break;
case ACMD_ENABLE:
pThis->u8State |= AUX_STATE_ENABLED;
#ifdef IN_RING3
ps2mSetDriverState(pThis, true);
#else
AssertLogRelMsgFailed(("Invalid ACMD_ENABLE outside R3!\n"));
#endif
ps2kClearQueue((GeneriQ *)&pThis->evtQ);
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
pThis->u8CurrCmd = 0;
break;
case ACMD_DISABLE:
pThis->u8State &= ~AUX_STATE_ENABLED;
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
pThis->u8CurrCmd = 0;
break;
case ACMD_SET_DEFAULT:
ps2mSetDefaults(pThis);
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
pThis->u8CurrCmd = 0;
break;
case ACMD_RESEND:
pThis->u8CurrCmd = 0;
break;
case ACMD_RESET:
ps2mSetDefaults(pThis);
///@todo reset more?
pThis->u8CurrCmd = cmd;
pThis->enmMode = AUX_MODE_RESET;
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
if (pThis->fDelayReset)
/* Slightly delay reset completion; it might take hundreds of ms. */
TMTimerSetMillies(pThis->CTX_SUFF(pDelayTimer), 1);
else
#ifdef IN_RING3
ps2mReset(pThis);
#else
AssertLogRelMsgFailed(("Invalid ACMD_RESET outside R3!\n"));
#endif
break;
/* The following commands need a parameter. */
case ACMD_SET_RES:
case ACMD_SET_SAMP_RATE:
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
pThis->u8CurrCmd = cmd;
break;
default:
/* Sending a command instead of a parameter starts the new command. */
switch (pThis->u8CurrCmd)
{
case ACMD_SET_RES:
//@todo reject unsupported resolutions
pThis->u8Resolution = cmd;
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
pThis->u8CurrCmd = 0;
break;
case ACMD_SET_SAMP_RATE:
//@todo reject unsupported rates
ps2mSetRate(pThis, cmd);
ps2mRateProtocolKnock(pThis, cmd);
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_ACK);
pThis->u8CurrCmd = 0;
break;
default:
fHandled = false;
}
/* Fall through only to handle unrecognized commands. */
if (fHandled)
break;
case ACMD_INVALID_1:
case ACMD_INVALID_2:
case ACMD_INVALID_3:
case ACMD_INVALID_4:
case ACMD_INVALID_5:
case ACMD_INVALID_6:
case ACMD_INVALID_7:
case ACMD_INVALID_8:
case ACMD_INVALID_9:
case ACMD_INVALID_10:
Log(("Unsupported command 0x%02X!\n", cmd));
ps2kInsertQueue((GeneriQ *)&pThis->cmdQ, ARSP_RESEND);
pThis->u8CurrCmd = 0;
break;
}
LogFlowFunc(("Active cmd now 0x%02X; updating interrupts\n", pThis->u8CurrCmd));
// KBCUpdateInterrupts(pThis->pParent);
return VINF_SUCCESS;
}
/**
* Send a byte (keystroke or command response) to the keyboard controller.
*
* @returns VINF_SUCCESS or VINF_TRY_AGAIN.
* @param pThis The PS/2 auxiliary device instance data.
* @param pb Where to return the byte we've read.
* @remarks Caller must have entered the device critical section.
*/
int PS2MByteFromAux(PPS2M pThis, uint8_t *pb)
{
int rc;
AssertPtr(pb);
/* Anything in the command queue has priority over data
* in the event queue. Additionally, keystrokes are //@todo: true?
* blocked if a command is currently in progress, even if
* the command queue is empty.
*/
//@todo: Probably should flush/not fill queue if stream mode reporting disabled?!
rc = ps2kRemoveQueue((GeneriQ *)&pThis->cmdQ, pb);
if (rc != VINF_SUCCESS && !pThis->u8CurrCmd && (pThis->u8State & AUX_STATE_ENABLED))
rc = ps2kRemoveQueue((GeneriQ *)&pThis->evtQ, pb);
LogFlowFunc(("mouse sends 0x%02x (%svalid data)\n", *pb, rc == VINF_SUCCESS ? "" : "not "));
//if (rc == VINF_SUCCESS) LogRel(("aux: sends 0x%02X\n", *pb));
return rc;
}
#ifdef IN_RING3
/* Event rate throttling timer to emulate the auxiliary device sampling rate.
*/
static DECLCALLBACK(void) ps2mThrottleTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
{
PPS2M pThis = (PS2M *)pvUser; NOREF(pDevIns);
uint32_t uHaveEvents;
/* Grab the lock to avoid races with PutEvent(). */
int rc = PDMCritSectEnter(pThis->pCritSectR3, VERR_SEM_BUSY);
AssertReleaseRC(rc);
#if 0
/* If the input queue is not empty, restart the timer. */
#else
/* If more movement is accumulated, report it and restart the timer. */
uHaveEvents = pThis->iAccumX | pThis->iAccumY | pThis->iAccumZ | pThis->fAccumB;
LogFlowFunc(("Have%s events\n", uHaveEvents ? "" : " no"));
if (uHaveEvents)
#endif
{
/* Report accumulated data, poke the KBC, and start the timer. */
ps2mReportAccumulatedEvents(pThis, (GeneriQ *)&pThis->evtQ, true);
KBCUpdateInterrupts(pThis->pParent);
TMTimerSetMillies(pThis->CTX_SUFF(pThrottleTimer), pThis->uThrottleDelay);
}
else
pThis->fThrottleActive = false;
PDMCritSectLeave(pThis->pCritSectR3);
}
/* The auxiliary device is specified to take up to about 500 milliseconds. We need
* to delay sending the result to the host for at least a tiny little while.
*/
static DECLCALLBACK(void) ps2mDelayTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
{
PPS2M pThis = (PS2M *)pvUser; NOREF(pDevIns);
LogFlowFunc(("Delay timer: cmd %02X\n", pThis->u8CurrCmd));
Assert(pThis->u8CurrCmd == ACMD_RESET);
ps2mReset(pThis);
///@todo Might want a PS2MCompleteCommand() to push last response, clear command, and kick the KBC...
/* Give the KBC a kick. */
KBCUpdateInterrupts(pThis->pParent);
}
/**
* Debug device info handler. Prints basic auxiliary device state.
*
* @param pDevIns Device instance which registered the info.
* @param pHlp Callback functions for doing output.
* @param pszArgs Argument string. Optional and specific to the handler.
*/
static DECLCALLBACK(void) ps2mInfoState(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
{
static const char *pcszModes[] = { "normal", "reset", "wrap" };
static const char *pcszProtocols[] = { "PS/2", NULL, NULL, "ImPS/2", "ImEx" };
PPS2M pThis = KBDGetPS2MFromDevIns(pDevIns);
NOREF(pszArgs);
Assert(pThis->enmMode <= RT_ELEMENTS(pcszModes));
Assert(pThis->enmProtocol <= RT_ELEMENTS(pcszProtocols));
pHlp->pfnPrintf(pHlp, "PS/2 mouse state: %s, %s mode, reporting %s\n",
pcszModes[pThis->enmMode],
pThis->u8State & AUX_STATE_REMOTE ? "remote" : "stream",
pThis->u8State & AUX_STATE_ENABLED ? "enabled" : "disabled");
pHlp->pfnPrintf(pHlp, "Protocol: %s, scaling %u:1\n",
pcszProtocols[pThis->enmProtocol], pThis->u8State & AUX_STATE_SCALING ? 2 : 1);
pHlp->pfnPrintf(pHlp, "Active command %02X\n", pThis->u8CurrCmd);
pHlp->pfnPrintf(pHlp, "Sampling rate %u reports/sec, resolution %u counts/mm\n",
pThis->u8SampleRate, 1 << pThis->u8Resolution);
pHlp->pfnPrintf(pHlp, "Command queue: %d items (%d max)\n",
pThis->cmdQ.cUsed, pThis->cmdQ.cSize);
pHlp->pfnPrintf(pHlp, "Event queue : %d items (%d max)\n",
pThis->evtQ.cUsed, pThis->evtQ.cSize);
}
/* -=-=-=-=-=- Mouse: IBase -=-=-=-=-=- */
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
static DECLCALLBACK(void *) ps2mQueryInterface(PPDMIBASE pInterface, const char *pszIID)
{
PPS2M pThis = RT_FROM_MEMBER(pInterface, PS2M, Mouse.IBase);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Mouse.IBase);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThis->Mouse.IPort);
return NULL;
}
/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
/**
* Mouse event handler.
*
* @returns VBox status code.
* @param pThis The PS/2 auxiliary device instance data.
* @param dx X direction movement delta.
* @param dy Y direction movement delta.
* @param dz Z (vertical scroll) movement delta.
* @param dw W (horizontal scroll) movement delta.
* @param fButtons Depressed button mask.
*/
static int ps2mPutEventWorker(PPS2M pThis, int32_t dx, int32_t dy,
int32_t dz, int32_t dw, uint32_t fButtons)
{
int rc = VINF_SUCCESS;
/* Update internal accumulators and button state. */
pThis->iAccumX += dx;
pThis->iAccumY += dy;
pThis->iAccumZ += dz;
pThis->fAccumB |= fButtons; //@todo: accumulate based on current protocol?
pThis->fCurrB = fButtons;
#if 1
/* Report the event and start the throttle timer unless it's already running. */
if (!pThis->fThrottleActive)
{
ps2mReportAccumulatedEvents(pThis, (GeneriQ *)&pThis->evtQ, true);
KBCUpdateInterrupts(pThis->pParent);
pThis->fThrottleActive = true;
TMTimerSetMillies(pThis->CTX_SUFF(pThrottleTimer), pThis->uThrottleDelay);
}
#else
/* Clamp the delta values to the allowed range. */
dx = RT_MIN(RT_MAX(dx, -256), 255);
dy = RT_MIN(RT_MAX(dy, -256), 255);
/* Start with the sync bit. */
val = RT_BIT(3);
/* Add buttons 1-3. */
val |= fButtons & PS2M_STD_BTN_MASK;
/* Set the X/Y sign bits. */
if (dx < 0)
val |= RT_BIT(4);
if (dy < 0)
val |= RT_BIT(5);
ps2kInsertQueue((GeneriQ *)&pThis->evtQ, val);
ps2kInsertQueue((GeneriQ *)&pThis->evtQ, (uint8_t)dx);
ps2kInsertQueue((GeneriQ *)&pThis->evtQ, (uint8_t)dy);
if (pThis->enmProtocol > PS2M_PROTO_PS2STD)
{
ps2kInsertQueue((GeneriQ *)&pThis->evtQ, (uint8_t)dz);
}
#endif
return rc;
}
/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
/**
* @interface_method_impl{PDMIMOUSEPORT, pfnPutEvent}
*/
static DECLCALLBACK(int) ps2mPutEvent(PPDMIMOUSEPORT pInterface, int32_t dx, int32_t dy,
int32_t dz, int32_t dw, uint32_t fButtons)
{
PPS2M pThis = RT_FROM_MEMBER(pInterface, PS2M, Mouse.IPort);
int rc = PDMCritSectEnter(pThis->pCritSectR3, VERR_SEM_BUSY);
AssertReleaseRC(rc);
LogFlowFunc(("dX=%d dY=%d dZ=%d dW=%d buttons=%02X\n", dx, dy, dz, dw, fButtons));
/* NB: The PS/2 Y axis direction is inverted relative to ours. */
ps2mPutEventWorker(pThis, dx, -dy, dz, dw, fButtons);
PDMCritSectLeave(pThis->pCritSectR3);
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMIMOUSEPORT, pfnPutEventAbs}
*/
static DECLCALLBACK(int) ps2mPutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t x, uint32_t y,
int32_t dz, int32_t dw, uint32_t fButtons)
{
AssertFailedReturn(VERR_NOT_SUPPORTED);
NOREF(pInterface); NOREF(x); NOREF(y); NOREF(dz); NOREF(dw); NOREF(fButtons);
}
/**
* @interface_method_impl{PDMIMOUSEPORT, pfnPutEventMultiTouch}
*/
static DECLCALLBACK(int) ps2mPutEventMT(PPDMIMOUSEPORT pInterface, uint8_t cContacts,
const uint64_t *pau64Contacts, uint32_t u32ScanTime)
{
AssertFailedReturn(VERR_NOT_SUPPORTED);
NOREF(pInterface); NOREF(cContacts); NOREF(pau64Contacts); NOREF(u32ScanTime);
}
/**
* Attach command.
*
* This is called to let the device attach to a driver for a
* specified LUN.
*
* This is like plugging in the mouse after turning on the
* system.
*
* @returns VBox status code.
* @param pThis The PS/2 auxiliary device instance data.
* @param pDevIns The device instance.
* @param iLUN The logical unit which is being detached.
* @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
*/
int PS2MAttach(PPS2M pThis, PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
{
int rc;
/* The LUN must be 1, i.e. mouse. */
Assert(iLUN == 1);
AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
("PS/2 mouse does not support hotplugging\n"),
VERR_INVALID_PARAMETER);
LogFlowFunc(("iLUN=%d\n", iLUN));
rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThis->Mouse.IBase, &pThis->Mouse.pDrvBase, "Mouse Port");
if (RT_SUCCESS(rc))
{
pThis->Mouse.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Mouse.pDrvBase, PDMIMOUSECONNECTOR);
if (!pThis->Mouse.pDrv)
{
AssertLogRelMsgFailed(("LUN #1 doesn't have a mouse interface! rc=%Rrc\n", rc));
rc = VERR_PDM_MISSING_INTERFACE;
}
}
else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
{
Log(("%s/%d: warning: no driver attached to LUN #1!\n", pDevIns->pReg->szName, pDevIns->iInstance));
rc = VINF_SUCCESS;
}
else
AssertLogRelMsgFailed(("Failed to attach LUN #1! rc=%Rrc\n", rc));
return rc;
}
void PS2MSaveState(PPS2M pThis, PSSMHANDLE pSSM)
{
uint32_t cPressed = 0;
LogFlowFunc(("Saving PS2M state\n"));
/* Save the core auxiliary device state. */
SSMR3PutU8(pSSM, pThis->u8State);
SSMR3PutU8(pSSM, pThis->u8SampleRate);
SSMR3PutU8(pSSM, pThis->u8Resolution);
SSMR3PutU8(pSSM, pThis->u8CurrCmd);
SSMR3PutU8(pSSM, pThis->enmMode);
SSMR3PutU8(pSSM, pThis->enmProtocol);
SSMR3PutU8(pSSM, pThis->enmKnockState);
/* Save the command and event queues. */
ps2kSaveQueue(pSSM, (GeneriQ *)&pThis->cmdQ);
ps2kSaveQueue(pSSM, (GeneriQ *)&pThis->evtQ);
/* Save the command delay timer. Note that the rate throttling
* timer is *not* saved.
*/
TMR3TimerSave(pThis->CTX_SUFF(pDelayTimer), pSSM);
}
int PS2MLoadState(PPS2M pThis, PSSMHANDLE pSSM, uint32_t uVersion)
{
uint8_t u8;
int rc;
NOREF(uVersion);
LogFlowFunc(("Loading PS2M state version %u\n", uVersion));
/* Load the basic auxiliary device state. */
SSMR3GetU8(pSSM, &pThis->u8State);
SSMR3GetU8(pSSM, &pThis->u8SampleRate);
SSMR3GetU8(pSSM, &pThis->u8Resolution);
SSMR3GetU8(pSSM, &pThis->u8CurrCmd);
SSMR3GetU8(pSSM, &u8);
pThis->enmMode = (PS2M_MODE)u8;
SSMR3GetU8(pSSM, &u8);
pThis->enmProtocol = (PS2M_PROTO)u8;
SSMR3GetU8(pSSM, &u8);
pThis->enmKnockState = (PS2M_KNOCK_STATE)u8;
/* Load the command and event queues. */
rc = ps2kLoadQueue(pSSM, (GeneriQ *)&pThis->cmdQ);
AssertRCReturn(rc, rc);
rc = ps2kLoadQueue(pSSM, (GeneriQ *)&pThis->evtQ);
AssertRCReturn(rc, rc);
/* Load the command delay timer, just in case. */
rc = TMR3TimerLoad(pThis->CTX_SUFF(pDelayTimer), pSSM);
AssertRCReturn(rc, rc);
/* Recalculate the throttling delay. */
ps2mSetRate(pThis, pThis->u8SampleRate);
ps2mSetDriverState(pThis, !!(pThis->u8State & AUX_STATE_ENABLED));
return rc;
}
void PS2MFixupState(PPS2M pThis, uint8_t u8State, uint8_t u8Rate, uint8_t u8Proto)
{
LogFlowFunc(("Fixing up old PS2M state version\n"));
/* Load the basic auxiliary device state. */
pThis->u8State = u8State;
pThis->u8SampleRate = u8Rate;
pThis->enmProtocol = (PS2M_PROTO)u8Proto;
/* Recalculate the throttling delay. */
ps2mSetRate(pThis, pThis->u8SampleRate);
ps2mSetDriverState(pThis, !!(pThis->u8State & AUX_STATE_ENABLED));
}
void PS2MReset(PPS2M pThis)
{
LogFlowFunc(("Resetting PS2M\n"));
pThis->u8CurrCmd = 0;
/* Clear the queues. */
ps2kClearQueue((GeneriQ *)&pThis->cmdQ);
ps2mSetDefaults(pThis); /* Also clears event queue. */
/* Activate the PS/2 mouse by default. */
// if (pThis->Mouse.pDrv)
// pThis->Mouse.pDrv->pfnSetActive(pThis->Mouse.pDrv, true);
}
void PS2MRelocate(PPS2M pThis, RTGCINTPTR offDelta, PPDMDEVINS pDevIns)
{
LogFlowFunc(("Relocating PS2M\n"));
pThis->pDelayTimerRC = TMTimerRCPtr(pThis->pDelayTimerR3);
pThis->pThrottleTimerRC = TMTimerRCPtr(pThis->pThrottleTimerR3);
NOREF(offDelta);
}
int PS2MConstruct(PPS2M pThis, PPDMDEVINS pDevIns, void *pParent, int iInstance)
{
int rc;
LogFlowFunc(("iInstance=%d\n", iInstance));
pThis->pParent = pParent;
/* Initialize the queues. */
pThis->evtQ.cSize = AUX_EVT_QUEUE_SIZE;
pThis->cmdQ.cSize = AUX_CMD_QUEUE_SIZE;
pThis->Mouse.IBase.pfnQueryInterface = ps2mQueryInterface;
pThis->Mouse.IPort.pfnPutEvent = ps2mPutEvent;
pThis->Mouse.IPort.pfnPutEventAbs = ps2mPutEventAbs;
pThis->Mouse.IPort.pfnPutEventMultiTouch = ps2mPutEventMT;
/*
* Initialize the critical section pointer(s).
*/
pThis->pCritSectR3 = pDevIns->pCritSectRoR3;
/*
* Create the input rate throttling timer. Does not use virtual time!
*/
PTMTIMER pTimer;
rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_REAL, ps2mThrottleTimer, pThis,
TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2M Throttle Timer", &pTimer);
if (RT_FAILURE(rc))
return rc;
pThis->pThrottleTimerR3 = pTimer;
pThis->pThrottleTimerR0 = TMTimerR0Ptr(pTimer);
pThis->pThrottleTimerRC = TMTimerRCPtr(pTimer);
/*
* Create the command delay timer.
*/
rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, ps2mDelayTimer, pThis,
TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "PS2M Delay Timer", &pTimer);
if (RT_FAILURE(rc))
return rc;
pThis->pDelayTimerR3 = pTimer;
pThis->pDelayTimerR0 = TMTimerR0Ptr(pTimer);
pThis->pDelayTimerRC = TMTimerRCPtr(pTimer);
/*
* Register debugger info callbacks.
*/
PDMDevHlpDBGFInfoRegister(pDevIns, "ps2m", "Display PS/2 mouse state.", ps2mInfoState);
//@todo: Where should we do this?
ps2mSetDriverState(pThis, true);
pThis->u8State = 0;
pThis->enmMode = AUX_MODE_STD;
return rc;
}
#endif
#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */