DevPS2.cpp revision f7f5cd7b1e530eb5636da51c974b48ae0c1775b3
/* $Id$ */
/** @file
* DevPS2 - PS/2 keyboard & mouse controller device.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
* --------------------------------------------------------------------
*
* This code is based on:
*
* QEMU PC keyboard emulation (revision 1.12)
*
* Copyright (c) 2003 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_KBD
#include "vl_vbox.h"
#include "VBoxDD.h"
#define PCKBD_SAVED_STATE_VERSION 5
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
PDMBOTHCBDECL(int) kbdIOPortDataRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
PDMBOTHCBDECL(int) kbdIOPortDataWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
PDMBOTHCBDECL(int) kbdIOPortStatusRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
PDMBOTHCBDECL(int) kbdIOPortCommandWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
/* debug PC keyboard */
#define DEBUG_KBD
/* debug PC keyboard : only mouse */
#define DEBUG_MOUSE
/* Keyboard Controller Commands */
#define KBD_CCMD_WRITE_OBUF 0xD2
initiated by the auxiliary device */
#define KBD_CCMD_RESET 0xFE
/* Keyboard Commands */
#define KBD_CMD_ECHO 0xEE
/* Keyboard Replies */
/* Status Register Bits */
/* Controller Mode Register Bits */
#define KBD_MODE_RFU 0x80
/* Mouse Commands */
#define AUX_SET_DEFAULT 0xF6
#define MOUSE_STATUS_REMOTE 0x40
#define MOUSE_STATUS_ENABLED 0x20
#define MOUSE_STATUS_SCALE21 0x10
#define KBD_QUEUE_SIZE 256
/** Supported mouse protocols */
enum
{
MOUSE_PROT_PS2 = 0,
MOUSE_PROT_IMPS2 = 3,
MOUSE_PROT_IMEX = 4
};
/** @name Mouse flags */
/** @{ */
/** IMEX horizontal scroll-wheel mode is active */
# define MOUSE_REPORT_HORIZONTAL 0x01
/** @} */
typedef struct {
} KBDQueue;
#define MOUSE_CMD_QUEUE_SIZE 8
typedef struct {
#define MOUSE_EVENT_QUEUE_SIZE 256
typedef struct {
typedef struct KBDState {
/* keyboard state */
/* mouse state */
/** Pointer to the device instance - RC. */
/** Pointer to the device instance - R3 . */
/** Pointer to the device instance. */
/** Critical section protecting the state. */
/**
* Keyboard port - LUN#0.
*
* @implements PDMIBASE
* @implements PDMIKEYBOARDPORT
*/
struct
{
/** The base interface for the keyboard port. */
/** The keyboard port base interface. */
/** The base interface of the attached keyboard driver. */
/** The keyboard interface of the attached keyboard driver. */
} Keyboard;
/**
* Mouse port - LUN#1.
*
* @implements PDMIBASE
* @implements PDMIMOUSEPORT
*/
struct
{
/** The base interface for the mouse port. */
/** The mouse port base interface. */
/** The base interface of the attached mouse driver. */
/** The mouse interface of the attached mouse driver. */
} Mouse;
} KBDState;
/* Table to convert from PC scancodes to scan code set 2. */
static const unsigned char ps2_raw_keycode_set2[128] = {
0,118, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85,102, 13,
21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 20, 28, 27,
35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 93, 26, 34, 33, 42,
50, 49, 58, 65, 73, 74, 89,124, 17, 41, 88, 5, 6, 4, 12, 3,
11, 2, 10, 1, 9,119,126,108,117,125,123,107,115,116,121,105,
114,122,112,113,127, 96, 97,120, 7, 15, 23, 31, 39, 47, 55, 63,
71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87,111,
19, 25, 57, 81, 83, 92, 95, 98, 99,100,101,103,104,106,109,110
};
/* Table to convert from PC scancodes to scan code set 3. */
static const unsigned char ps2_raw_keycode_set3[128] = {
0,118, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85,102, 13,
21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 17, 28, 27,
35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 92, 26, 34, 33, 42,
50, 49, 58, 65, 73, 74, 89,124, 25, 41, 20, 5, 6, 4, 12, 3,
11, 2, 10, 1, 9,118,126,108,117,125,123,107,115,116,121,105,
114,122,112,113,127, 96, 19,120, 7, 15, 23, 31, 39, 47, 55, 63,
71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87,111,
19, 25, 57, 81, 83, 92, 95, 98, 99,100,101,103,104,106,109,110
};
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
/* update irq and KBD_STAT_[MOUSE_]OBF */
static void kbd_update_irq(KBDState *s)
{
int irq12_level, irq1_level;
irq1_level = 0;
irq12_level = 0;
/* Determine new OBF state, but only if OBF is clear. If OBF was already
* set, we cannot risk changing the event type after an ISR potentially
* started executing! Only kbd_read_data() clears the OBF bits.
*/
if (!(s->status & KBD_STAT_OBF)) {
s->status &= ~KBD_STAT_MOUSE_OBF;
/* Keyboard data has priority if both kbd and aux data is available. */
{
s->status |= KBD_STAT_OBF;
if (++q->rptr == KBD_QUEUE_SIZE)
q->rptr = 0;
q->count--;
}
{
{
}
else
{
}
}
}
/* Determine new IRQ state. */
if (s->status & KBD_STAT_OBF) {
if (s->status & KBD_STAT_MOUSE_OBF)
{
if (s->mode & KBD_MODE_MOUSE_INT)
irq12_level = 1;
}
else
{ /* KBD_STAT_OBF set but KBD_STAT_MOUSE_OBF isn't. */
if (s->mode & KBD_MODE_KBD_INT)
irq1_level = 1;
}
}
}
{
#if defined(DEBUG_MOUSE) || defined(DEBUG_KBD)
if (aux == 1)
else if (aux == 2)
#ifdef DEBUG_KBD
else
#endif
#endif
switch (aux)
{
case 0: /* keyboard */
if (q->count >= KBD_QUEUE_SIZE)
return;
if (++q->wptr == KBD_QUEUE_SIZE)
q->wptr = 0;
q->count++;
break;
case 1: /* mouse command response */
return;
break;
case 2: /* mouse event data */
return;
break;
default:
}
kbd_update_irq(s);
}
#ifdef IN_RING3
{
/* XXX: add support for scancode sets 1 and 3 */
{
if (keycode & 0x80)
kbd_queue(s, 0xf0, 0);
if (s->scancode_set == 2)
else if (s->scancode_set == 3)
}
}
#endif /* IN_RING3 */
{
/* Set the OBF and raise IRQ. */
s->status |= KBD_STAT_OBF;
if (s->mode & KBD_MODE_KBD_INT)
}
{
#if defined(DEBUG_KBD)
#endif
return val;
}
{
int rc = VINF_SUCCESS;
#ifdef DEBUG_KBD
#endif
switch(val) {
case KBD_CCMD_READ_MODE:
kbc_dbb_out(s, s->mode);
break;
case KBD_CCMD_WRITE_MODE:
case KBD_CCMD_WRITE_OBUF:
case KBD_CCMD_WRITE_AUX_OBUF:
case KBD_CCMD_WRITE_MOUSE:
case KBD_CCMD_WRITE_OUTPORT:
break;
case KBD_CCMD_MOUSE_DISABLE:
s->mode |= KBD_MODE_DISABLE_MOUSE;
break;
case KBD_CCMD_MOUSE_ENABLE:
s->mode &= ~KBD_MODE_DISABLE_MOUSE;
/* Check for queued input. */
kbd_update_irq(s);
break;
case KBD_CCMD_TEST_MOUSE:
kbc_dbb_out(s, 0x00);
break;
case KBD_CCMD_SELF_TEST:
/* Enable the A20 line - that is the power-on state(!). */
# ifndef IN_RING3
{
break;
}
# else /* IN_RING3 */
# endif /* IN_RING3 */
s->status |= KBD_STAT_SELFTEST;
s->mode |= KBD_MODE_DISABLE_KBD;
kbc_dbb_out(s, 0x55);
break;
case KBD_CCMD_KBD_TEST:
kbc_dbb_out(s, 0x00);
break;
case KBD_CCMD_KBD_DISABLE:
s->mode |= KBD_MODE_DISABLE_KBD;
break;
case KBD_CCMD_KBD_ENABLE:
s->mode &= ~KBD_MODE_DISABLE_KBD;
/* Check for queued input. */
kbd_update_irq(s);
break;
case KBD_CCMD_READ_INPORT:
kbc_dbb_out(s, 0x00);
break;
case KBD_CCMD_READ_OUTPORT:
/* XXX: check that */
#ifdef TARGET_I386
#else
val = 0x01;
#endif
if (s->status & KBD_STAT_OBF)
val |= 0x10;
if (s->status & KBD_STAT_MOUSE_OBF)
val |= 0x20;
kbc_dbb_out(s, val);
break;
#ifdef TARGET_I386
case KBD_CCMD_ENABLE_A20:
# ifndef IN_RING3
# else /* IN_RING3 */
# endif /* IN_RING3 */
break;
case KBD_CCMD_DISABLE_A20:
# ifndef IN_RING3
# else /* IN_RING3 */
# endif /* !IN_RING3 */
break;
#endif
case KBD_CCMD_READ_TSTINP:
/* Keyboard clock line is zero IFF keyboard is disabled */
kbc_dbb_out(s, val);
break;
case KBD_CCMD_RESET:
#ifndef IN_RING3
#else /* IN_RING3 */
#endif /* !IN_RING3 */
break;
case 0xff:
/* ignore that - I don't know what is its use */
break;
/* Make OS/2 happy. */
/* The 8042 RAM is readable using commands 0x20 thru 0x3f, and writable
by 0x60 thru 0x7f. Now days only the firs byte, the mode, is used.
We'll ignore the writes (0x61..7f) and return 0 for all the reads
just to make some OS/2 debug stuff a bit happier. */
case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
case 0x28: case 0x29: case 0x2a: case 0x2b: case 0x2c: case 0x2d: case 0x2e: case 0x2f:
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
case 0x38: case 0x39: case 0x3a: case 0x3b: case 0x3c: case 0x3d: case 0x3e: case 0x3f:
kbc_dbb_out(s, 0);
break;
default:
break;
}
return rc;
}
{
/* Return the current DBB contents. */
/* Reading the DBB deasserts IRQs... */
if (s->status & KBD_STAT_MOUSE_OBF)
else
/* ...and clears the OBF bits. */
/* Check if more data is available. */
kbd_update_irq(s);
#ifdef DEBUG_KBD
#endif
return val;
}
static void kbd_reset_keyboard(KBDState *s)
{
s->scan_enabled = 1;
s->scancode_set = 2;
/* Flush the keyboard queue. */
}
{
switch(s->kbd_write_cmd) {
default:
case -1:
switch(val) {
case 0x00:
kbd_queue(s, KBD_REPLY_ACK, 0);
break;
case 0x05:
kbd_queue(s, KBD_REPLY_RESEND, 0);
break;
case KBD_CMD_GET_ID:
kbd_queue(s, KBD_REPLY_ACK, 0);
kbd_queue(s, 0xab, 0);
kbd_queue(s, 0x83, 0);
break;
case KBD_CMD_ECHO:
kbd_queue(s, KBD_CMD_ECHO, 0);
break;
case KBD_CMD_ENABLE:
s->scan_enabled = 1;
kbd_queue(s, KBD_REPLY_ACK, 0);
break;
case KBD_CMD_SCANCODE:
case KBD_CMD_SET_LEDS:
case KBD_CMD_SET_RATE:
s->kbd_write_cmd = val;
kbd_queue(s, KBD_REPLY_ACK, 0);
break;
case KBD_CMD_RESET_DISABLE:
s->scan_enabled = 0;
kbd_queue(s, KBD_REPLY_ACK, 0);
break;
case KBD_CMD_RESET_ENABLE:
s->scan_enabled = 1;
kbd_queue(s, KBD_REPLY_ACK, 0);
break;
case KBD_CMD_RESET:
kbd_queue(s, KBD_REPLY_ACK, 0);
kbd_queue(s, KBD_REPLY_POR, 0);
break;
default:
kbd_queue(s, KBD_REPLY_ACK, 0);
break;
}
break;
case KBD_CMD_SCANCODE:
#ifdef IN_RING3
if (val == 0) {
if (s->scancode_set == 1)
pc_kbd_put_keycode(s, 0x43);
else if (s->scancode_set == 2)
pc_kbd_put_keycode(s, 0x41);
else if (s->scancode_set == 3)
pc_kbd_put_keycode(s, 0x3f);
} else {
s->scancode_set = val;
}
kbd_queue(s, KBD_REPLY_ACK, 0);
}
#else
return VINF_IOM_HC_IOPORT_WRITE;
#endif
case KBD_CMD_SET_LEDS:
{
#ifdef IN_RING3
if (val & 0x01)
if (val & 0x02)
if (val & 0x04)
#else
return VINF_IOM_HC_IOPORT_WRITE;
#endif
kbd_queue(s, KBD_REPLY_ACK, 0);
s->kbd_write_cmd = -1;
}
break;
case KBD_CMD_SET_RATE:
kbd_queue(s, KBD_REPLY_ACK, 0);
s->kbd_write_cmd = -1;
break;
}
return VINF_SUCCESS;
}
{
}
/**
* Send a single relative packet in 3-byte PS/2 format to the PS/2 controller.
* @param s keyboard state object
* @param dx relative X value, must be between -256 and +255
* @param dy relative y value, must be between -256 and +255
* @param fButtonsLow the state of the two first mouse buttons
* @param fButtonsPacked the state of the upper three mouse buttons and
* scroll wheel movement, packed as per the
* MOUSE_EXT_* defines. For standard PS/2 packets
* only pass the value of button 3 here.
*/
{
unsigned int b;
LogRel3(("%s: dx1=%d, dy1=%d, fButtonsLow=0x%x\n",
| fButtonsLow;
}
{
}
{
if (s->mouse_dw > 0)
dw1 = 1;
else if (s->mouse_dw < 0)
dw1 = -1;
else if (s->mouse_dz > 0)
dz1 = 1;
else if (s->mouse_dz < 0)
dz1 = -1;
{
}
else
{
LogRel3(("%s: dz1=%d, dw1=%d, fButtonsHigh=0x%x\n",
: dw1 < 0 ? 7
: dz1 > 0 ? 1
: 0;
}
}
/**
* Send a single relative packet in (IM)PS/2 or IMEX format to the PS/2
* controller.
* @param s keyboard state object
* @param fToCmdQueue should this packet go to the command queue (or the
* event queue)?
*/
{
if (s->mouse_type == MOUSE_PROT_IMPS2)
if (s->mouse_type == MOUSE_PROT_IMEX)
}
#ifdef IN_RING3
static bool kbd_mouse_unreported(KBDState *s)
{
return s->mouse_dx
|| s->mouse_dy
|| s->mouse_dz
|| s->mouse_dw
|| s->mouse_buttons != s->mouse_buttons_reported;
}
{
}
int buttons_state)
{
LogRel3(("%s: dx=%d, dy=%d, dz=%d, dw=%d, buttons_state=0x%x\n",
/* check if deltas are recorded when disabled */
if (!(s->mouse_status & MOUSE_STATUS_ENABLED))
return;
if ( (s->mouse_type == MOUSE_PROT_IMPS2)
|| (s->mouse_type == MOUSE_PROT_IMEX))
if (s->mouse_type == MOUSE_PROT_IMEX)
s->mouse_buttons = buttons_state;
if (!(s->mouse_status & MOUSE_STATUS_REMOTE))
/* if not remote, send event. Multiple events are sent if
too big deltas */
while ( kbd_mouse_unreported(s)
&& kbd_mouse_event_queue_free(s) > 4)
kbd_mouse_send_packet(s, false);
}
/* Report a change in status down the driver chain */
{
}
#endif /* IN_RING3 */
{
#ifdef DEBUG_MOUSE
#endif
int rc = VINF_SUCCESS;
/* Flush the mouse command response queue. */
s->mouse_command_queue.count = 0;
s->mouse_command_queue.rptr = 0;
s->mouse_command_queue.wptr = 0;
switch(s->mouse_write_cmd) {
default:
case -1:
/* mouse command */
if (s->mouse_wrap) {
if (val == AUX_RESET_WRAP) {
s->mouse_wrap = 0;
return VINF_SUCCESS;
return VINF_SUCCESS;
}
}
switch(val) {
case AUX_SET_SCALE11:
s->mouse_status &= ~MOUSE_STATUS_SCALE21;
break;
case AUX_SET_SCALE21:
s->mouse_status |= MOUSE_STATUS_SCALE21;
break;
case AUX_SET_STREAM:
s->mouse_status &= ~MOUSE_STATUS_REMOTE;
break;
case AUX_SET_WRAP:
s->mouse_wrap = 1;
break;
case AUX_SET_REMOTE:
s->mouse_status |= MOUSE_STATUS_REMOTE;
break;
case AUX_GET_TYPE:
break;
case AUX_SET_RES:
case AUX_SET_SAMPLE:
s->mouse_write_cmd = val;
break;
case AUX_GET_SCALE:
break;
case AUX_POLL:
kbd_mouse_send_packet(s, true);
break;
case AUX_ENABLE_DEV:
#ifdef IN_RING3
LogRelFlowFunc(("Enabling mouse device\n"));
s->mouse_status |= MOUSE_STATUS_ENABLED;
#else
LogRelFlowFunc(("Enabling mouse device, R0 stub\n"));
#endif
break;
case AUX_DISABLE_DEV:
#ifdef IN_RING3
s->mouse_status &= ~MOUSE_STATUS_ENABLED;
/* Flush the mouse events queue. */
s->mouse_event_queue.count = 0;
s->mouse_event_queue.rptr = 0;
s->mouse_event_queue.wptr = 0;
#else
#endif
break;
case AUX_SET_DEFAULT:
#ifdef IN_RING3
s->mouse_sample_rate = 100;
s->mouse_resolution = 2;
s->mouse_status = 0;
#else
#endif
break;
case AUX_RESET:
#ifdef IN_RING3
s->mouse_sample_rate = 100;
s->mouse_resolution = 2;
s->mouse_status = 0;
s->mouse_type = MOUSE_PROT_PS2;
/* Flush the mouse events queue. */
s->mouse_event_queue.count = 0;
s->mouse_event_queue.rptr = 0;
s->mouse_event_queue.wptr = 0;
#else
#endif
break;
default:
/* NACK all commands we don't know.
The usecase for this is the OS/2 mouse driver which will try
read 0xE2 in order to figure out if it's a trackpoint device
or not. If it doesn't get a NACK (or ACK) on the command it'll
do several hundred thousand status reads before giving up. This
is slows down the OS/2 boot up considerably. (It also seems that
the code is somehow vulnerable while polling like this and that
mouse or keyboard input at this point might screw things up badly.)
Every command or data byte sent to the mouse (except for the
resend command fe) is ACKed with fa. If the command or data
is invalid, it is NACKed with fe. If the next byte is again
invalid, the reply is ERROR: fc. */
/** @todo send error if we NACKed the previous command? */
break;
}
break;
case AUX_SET_SAMPLE:
s->mouse_sample_rate = val;
/* detect IMPS/2 or IMEX */
/* And enable horizontal scrolling reporting when requested */
switch(s->mouse_detect_state) {
default:
case 0:
if (val == 200)
s->mouse_detect_state = 1;
break;
case 1:
if (val == 100)
s->mouse_detect_state = 2;
else if (val == 200)
s->mouse_detect_state = 3;
/* enable horizontal scrolling, byte two */
s->mouse_detect_state = 4;
else
s->mouse_detect_state = 0;
break;
case 2:
{
LogRelFlowFunc(("switching mouse device to IMPS/2 mode\n"));
s->mouse_type = MOUSE_PROT_IMPS2;
}
s->mouse_detect_state = 0;
break;
case 3:
if (val == 80)
{
LogRelFlowFunc(("switching mouse device to IMEX mode\n"));
s->mouse_type = MOUSE_PROT_IMEX;
}
s->mouse_detect_state = 0;
break;
case 4:
if (val == 40)
{
LogRelFlowFunc(("enabling IMEX horizontal scrolling reporting\n"));
}
s->mouse_detect_state = 0;
break;
}
s->mouse_write_cmd = -1;
break;
case AUX_SET_RES:
{
s->mouse_resolution = val;
}
else
s->mouse_write_cmd = -1;
break;
}
return rc;
}
{
int rc = VINF_SUCCESS;
#ifdef DEBUG_KBD
#endif
switch(s->write_cmd) {
case 0:
/* Automatically enables keyboard interface. */
s->mode &= ~KBD_MODE_DISABLE_KBD;
break;
case KBD_CCMD_WRITE_MODE:
kbd_update_irq(s);
break;
case KBD_CCMD_WRITE_OBUF:
kbc_dbb_out(s, val);
break;
case KBD_CCMD_WRITE_AUX_OBUF:
break;
case KBD_CCMD_WRITE_OUTPORT:
#ifdef TARGET_I386
# ifndef IN_RING3
# else /* IN_RING3 */
# endif /* !IN_RING3 */
#endif
if (!(val & 1)) {
# ifndef IN_RING3
# else
# endif
}
break;
case KBD_CCMD_WRITE_MOUSE:
break;
default:
break;
}
if (rc != VINF_IOM_HC_IOPORT_WRITE)
s->write_cmd = 0;
return rc;
}
#ifdef IN_RING3
{
KBDQueue *q;
s->kbd_write_cmd = -1;
s->mouse_write_cmd = -1;
/* Resetting everything, keyword was not working right on NT4 reboot. */
s->write_cmd = 0;
s->scan_enabled = 0;
s->translate = 0;
s->scancode_set = 2;
if (s->mouse_status)
{
s->mouse_status = 0;
}
s->mouse_resolution = 0;
s->mouse_sample_rate = 0;
s->mouse_wrap = 0;
s->mouse_type = MOUSE_PROT_PS2;
s->mouse_detect_state = 0;
s->mouse_dx = 0;
s->mouse_dy = 0;
s->mouse_dz = 0;
s->mouse_dw = 0;
s->mouse_flags = 0;
s->mouse_buttons = 0;
s->mouse_buttons_reported = 0;
q = &s->queue;
q->rptr = 0;
q->wptr = 0;
q->count = 0;
mcq = &s->mouse_command_queue;
meq = &s->mouse_event_queue;
}
{
int i;
qemu_put_8s(f, &s->write_cmd);
qemu_put_8s(f, &s->status);
qemu_put_8s(f, &s->mode);
qemu_put_be32s(f, &s->kbd_write_cmd);
qemu_put_be32s(f, &s->scan_enabled);
qemu_put_be32s(f, &s->mouse_write_cmd);
qemu_put_8s(f, &s->mouse_status);
qemu_put_8s(f, &s->mouse_resolution);
qemu_put_8s(f, &s->mouse_sample_rate);
qemu_put_8s(f, &s->mouse_wrap);
qemu_put_8s(f, &s->mouse_type);
qemu_put_8s(f, &s->mouse_detect_state);
qemu_put_be32s(f, &s->mouse_dx);
qemu_put_be32s(f, &s->mouse_dy);
qemu_put_be32s(f, &s->mouse_dz);
qemu_put_be32s(f, &s->mouse_dw);
qemu_put_be32s(f, &s->mouse_flags);
qemu_put_8s(f, &s->mouse_buttons);
qemu_put_8s(f, &s->mouse_buttons_reported);
/* XXX: s->scancode_set isn't being saved, but we only really support set 2,
* so no real harm done.
*/
/*
* We have to save the queues too.
*/
SSMR3PutU32(f, cItems);
SSMR3PutU32(f, cItems);
for (i = s->mouse_command_queue.rptr; cItems-- > 0; i = (i + 1) % RT_ELEMENTS(s->mouse_command_queue.data))
SSMR3PutU32(f, cItems);
for (i = s->mouse_event_queue.rptr; cItems-- > 0; i = (i + 1) % RT_ELEMENTS(s->mouse_event_queue.data))
/* terminator */
SSMR3PutU32(f, ~0);
}
{
int rc;
#if 0
/** @todo enable this and remove the "if (version_id == 4)" code at some
* later time */
/* Version 4 was never created by any publicly released version of VBox */
#endif
qemu_get_8s(f, &s->write_cmd);
qemu_get_8s(f, &s->status);
qemu_get_8s(f, &s->mode);
qemu_get_8s(f, &s->mouse_status);
qemu_get_8s(f, &s->mouse_resolution);
qemu_get_8s(f, &s->mouse_sample_rate);
qemu_get_8s(f, &s->mouse_wrap);
qemu_get_8s(f, &s->mouse_type);
qemu_get_8s(f, &s->mouse_detect_state);
if (version_id > 2)
{
SSMR3GetS32(f, &s->mouse_dw);
SSMR3GetS32(f, &s->mouse_flags);
}
qemu_get_8s(f, &s->mouse_buttons);
if (version_id == 4)
{
SSMR3GetU32(f, &u32Dummy);
SSMR3GetU32(f, &u32Dummy);
}
if (version_id > 3)
SSMR3GetU8(f, &s->mouse_buttons_reported);
if (version_id == 4)
SSMR3GetU8(f, &u8Dummy);
s->mouse_command_queue.count = 0;
s->mouse_command_queue.rptr = 0;
s->mouse_command_queue.wptr = 0;
s->mouse_event_queue.count = 0;
s->mouse_event_queue.rptr = 0;
s->mouse_event_queue.wptr = 0;
/* Determine the translation state. */
/*
* Load the queues
*/
if (RT_FAILURE(rc))
return rc;
{
}
for (i = 0; i < u32; i++)
{
if (RT_FAILURE(rc))
return rc;
}
if (RT_FAILURE(rc))
return rc;
{
}
for (i = 0; i < u32; i++)
{
if (RT_FAILURE(rc))
return rc;
}
if (RT_FAILURE(rc))
return rc;
{
}
for (i = 0; i < u32; i++)
{
if (RT_FAILURE(rc))
return rc;
}
/* terminator */
if (RT_FAILURE(rc))
return rc;
if (u32 != ~0U)
{
}
/* Resend a notification to Main if the device is active */
return 0;
}
#endif /* IN_RING3 */
/* VirtualBox code start */
/* -=-=-=-=-=- wrappers -=-=-=-=-=- */
/**
* Port I/O Handler for keyboard data IN operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - ignored.
* @param Port Port number used for the IN operation.
* @param pu32 Where to store the result.
* @param cb Number of bytes read.
*/
PDMBOTHCBDECL(int) kbdIOPortDataRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
{
if (cb == 1)
{
{
}
return rc;
}
return VERR_IOM_IOPORT_UNUSED;
}
/**
* Port I/O Handler for keyboard data OUT operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - ignored.
* @param Port Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
PDMBOTHCBDECL(int) kbdIOPortDataWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
{
int rc = VINF_SUCCESS;
if (cb == 1)
{
{
}
}
else
return rc;
}
/**
* Port I/O Handler for keyboard status IN operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - ignored.
* @param Port Port number used for the IN operation.
* @param pu32 Where to store the result.
* @param cb Number of bytes read.
*/
PDMBOTHCBDECL(int) kbdIOPortStatusRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
{
if (cb == 1)
{
{
}
return rc;
}
return VERR_IOM_IOPORT_UNUSED;
}
/**
* Port I/O Handler for keyboard command OUT operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - ignored.
* @param Port Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
PDMBOTHCBDECL(int) kbdIOPortCommandWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
{
int rc = VINF_SUCCESS;
if (cb == 1)
{
{
}
}
else
return rc;
}
#ifdef IN_RING3
/**
* Saves a state of the keyboard device.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSMHandle The handle to save the state to.
*/
{
return VINF_SUCCESS;
}
/**
* Loads a saved keyboard device state.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSMHandle The handle to the saved state.
* @param uVersion The data unit version number.
* @param uPass The data pass.
*/
static DECLCALLBACK(int) kbdLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t uVersion, uint32_t uPass)
{
}
/**
* Reset notification.
*
* @returns VBox status.
* @param pDevIns The device instance data.
*/
{
/* Activate the PS/2 keyboard by default. */
}
/* -=-=-=-=-=- Keyboard: IBase -=-=-=-=-=- */
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
{
return NULL;
}
/* -=-=-=-=-=- Keyboard: IKeyboardPort -=-=-=-=-=- */
/**
* Keyboard event handler.
*
* @returns VBox status code.
* @param pInterface Pointer to the keyboard port interface (KBDState::Keyboard.IPort).
* @param u8KeyCode The keycode.
*/
{
return VINF_SUCCESS;
}
/* -=-=-=-=-=- Mouse: IBase -=-=-=-=-=- */
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
{
return NULL;
}
/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
/**
* @interface_method_impl{PDMIMOUSEPORT, pfnPutEvent}
*/
static DECLCALLBACK(int) kbdMousePutEvent(PPDMIMOUSEPORT pInterface, int32_t iDeltaX, int32_t iDeltaY,
{
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMIMOUSEPORT, pfnPutEventAbs}
*/
static DECLCALLBACK(int) kbdMousePutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t uX, uint32_t uY, int32_t iDeltaZ, int32_t iDeltaW, uint32_t fButtons)
{
}
/* -=-=-=-=-=- real code -=-=-=-=-=- */
/**
* Attach command.
*
* This is called to let the device attach to a driver for a specified LUN
* during runtime. This is not called during VM construction, the device
* constructor have to attach to all the available drivers.
*
* This is like plugging in the keyboard or mouse after turning on the PC.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param iLUN The logical unit which is being detached.
* @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
* @remark The keyboard controller doesn't support this action, this is just
* implemented to try out the driver<->device structure.
*/
{
int rc;
("PS/2 device does not support hotplugging\n"),
switch (iLUN)
{
/* LUN #0: keyboard */
case 0:
rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThis->Keyboard.IBase, &pThis->Keyboard.pDrvBase, "Keyboard Port");
if (RT_SUCCESS(rc))
{
{
}
}
else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
{
Log(("%s/%d: warning: no driver attached to LUN #0!\n", pDevIns->pReg->szName, pDevIns->iInstance));
rc = VINF_SUCCESS;
}
else
break;
case 1:
rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThis->Mouse.IBase, &pThis->Mouse.pDrvBase, "Aux (Mouse) Port");
if (RT_SUCCESS(rc))
{
{
}
}
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
break;
default:
return VERR_PDM_NO_SUCH_LUN;
}
return rc;
}
/**
* Detach notification.
*
* This is called when a driver is detaching itself from a LUN of the device.
* The device should adjust it's state to reflect this.
*
* This is like unplugging the network cable to use it for the laptop or
* something while the PC is still running.
*
* @param pDevIns The device instance.
* @param iLUN The logical unit which is being detached.
* @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
* @remark The keyboard controller doesn't support this action, this is just
* implemented to try out the driver<->device structure.
*/
{
#if 0
/*
* Reset the interfaces and update the controller state.
*/
switch (iLUN)
{
/* LUN #0: keyboard */
case 0:
break;
case 1:
break;
default:
break;
}
#else
#endif
}
/**
* @copydoc FNPDMDEVRELOCATE
*/
{
}
/**
* Destruct a device instance for a VM.
*
* @returns VBox status.
* @param pDevIns The device instance data.
*/
{
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMDEVREG,pfnConstruct}
*/
{
int rc;
bool fGCEnabled;
bool fR0Enabled;
/*
* Validate and read the configuration.
*/
if (RT_FAILURE(rc))
if (RT_FAILURE(rc))
/*
* Initialize the interfaces.
*/
/*
* Initialize the critical section.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Register I/O ports, save state, keyboard event handler and mouse event handlers.
*/
rc = PDMDevHlpIOPortRegister(pDevIns, 0x60, 1, NULL, kbdIOPortDataWrite, kbdIOPortDataRead, NULL, NULL, "PC Keyboard - Data");
if (RT_FAILURE(rc))
return rc;
rc = PDMDevHlpIOPortRegister(pDevIns, 0x64, 1, NULL, kbdIOPortCommandWrite, kbdIOPortStatusRead, NULL, NULL, "PC Keyboard - Command / Status");
if (RT_FAILURE(rc))
return rc;
if (fGCEnabled)
{
rc = PDMDevHlpIOPortRegisterRC(pDevIns, 0x60, 1, 0, "kbdIOPortDataWrite", "kbdIOPortDataRead", NULL, NULL, "PC Keyboard - Data");
if (RT_FAILURE(rc))
return rc;
rc = PDMDevHlpIOPortRegisterRC(pDevIns, 0x64, 1, 0, "kbdIOPortCommandWrite", "kbdIOPortStatusRead", NULL, NULL, "PC Keyboard - Command / Status");
if (RT_FAILURE(rc))
return rc;
}
if (fR0Enabled)
{
rc = PDMDevHlpIOPortRegisterR0(pDevIns, 0x60, 1, 0, "kbdIOPortDataWrite", "kbdIOPortDataRead", NULL, NULL, "PC Keyboard - Data");
if (RT_FAILURE(rc))
return rc;
rc = PDMDevHlpIOPortRegisterR0(pDevIns, 0x64, 1, 0, "kbdIOPortCommandWrite", "kbdIOPortStatusRead", NULL, NULL, "PC Keyboard - Command / Status");
if (RT_FAILURE(rc))
return rc;
}
rc = PDMDevHlpSSMRegister(pDevIns, PCKBD_SAVED_STATE_VERSION, sizeof(*pThis), kbdSaveExec, kbdLoadExec);
if (RT_FAILURE(rc))
return rc;
/*
* Attach to the keyboard and mouse drivers.
*/
if (RT_FAILURE(rc))
return rc;
if (RT_FAILURE(rc))
return rc;
/*
* Initialize the device state.
*/
return VINF_SUCCESS;
}
/**
* The device registration structure.
*/
const PDMDEVREG g_DevicePS2KeyboardMouse =
{
/* u32Version */
/* szName */
"pckbd",
/* szRCMod */
"VBoxDDGC.gc",
/* szR0Mod */
"VBoxDDR0.r0",
/* pszDescription */
"PS/2 Keyboard and Mouse device. Emulates both the keyboard, mouse and the keyboard controller. "
"LUN #0 is the keyboard connector. "
/* fFlags */
PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
/* fClass */
/* cMaxInstances */
1,
/* cbInstance */
sizeof(KBDState),
/* pfnConstruct */
/* pfnDestruct */
/* pfnRelocate */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
/* pfnDetach */
/* pfnQueryInterface. */
NULL,
/* pfnInitComplete */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32VersionEnd */
};
#endif /* IN_RING3 */
#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */