BusMouse.cpp revision cf9e70141e506c10f2b04c3269b1e9e98b336564
/* $Id$ */
/** @file
* BusMouse - Microsoft Bus (parallel) mouse controller device.
*/
/*
* Copyright (C) 2006-2013 Oracle Corporation
*
* 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 to use,
* 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
/** @page pg_busmouse DevBusMouse - Microsoft Bus Mouse Emulation
*
* The Microsoft Bus Mouse was an early mouse sold by Microsoft, originally
* introduced in 1983. The mouse had a D-shaped 9-pin connector which plugged
* into a small ISA add-in board.
*
* The mouse itself was very simple (compared to a serial mouse) and most of the
* logic was located on the ISA board. Later, Microsoft sold an InPort mouse,
* which was also called a "bus mouse", but used a different interface.
*
* Microsoft part numbers for the Bus Mouse were 037-099 (100 ppi)
* and 037-199 (200 ppi).
*
* The Bus Mouse adapter included IRQ configuration jumpers (ref. MS article
* Q12230). The IRQ could be set to one of 2, 3, 4, 5. The typical setting
* may conflict with a SoundBlaster or a PCI device, this device defaults to
* IRQ 3. Note that IRQ 3 is also used by the COM 2 device, not often needed.
*
* The ISA adapter was built around an Intel 8255A compatible chip (ref.
* MS article Q46369). Once enabled, the adapter raises the configured IRQ
* 30 times per second; the rate is not configurable. The interrupts
* occur regardless of whether the mouse state has changed or not.
*
* To function properly, the 8255A must be programmed as follows:
* - Port A: Input. Used to read motion deltas and button states.
* - Port B: Output. Not used except for mouse detection.
* - Port C: Split. Upper bits set as output, used for control purposes.
* Lower bits set as input, reflecting IRQ state.
*
* Detailed information was gleaned from Windows and OS/2 DDK mouse samples.
*/
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The original bus mouse controller is fixed at I/O port 0x23C. */
#define BMS_IO_BASE 0x23C
#define BMS_IO_SIZE 4
/** @name Offsets relative to the I/O base.
*@{ */
#define BMS_PORT_DATA 0 /**< 8255 Port A. */
/** @} */
/** @name Port C bits (control port).
* @{ */
/** @} */
/** @name Port A bits (data port).
* @{ */
/** @} */
/** Convert IRQ level (2/3/4/5) to a bit in the control register. */
/** IRQ period, corresponds to approx. 30 Hz. */
#define BMS_IRQ_PERIOD_MS 34
/** Default IRQ setting. */
#define BMS_DEFAULT_IRQ 3
/** The saved state version. */
#define BMS_SAVED_STATE_VERSION 1
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The device state.
*/
typedef struct MouState
{
/** @name 8255A state
* @{ */
/** Mouse timer handle - HC. */
/** Timer period in milliseconds. */
/** @} */
/** @name mouse state
* @{ */
/** @} */
/** Pointer to the device instance - RC. */
/** Pointer to the device instance - R3 . */
/** Pointer to the device instance. */
/**
* Mouse port - LUN#0.
*
* @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;
} MouState;
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
# ifdef IN_RING3
/**
* Report a change in status down the driver chain.
*
* We want to report the mouse as enabled if and only if the guest is "using"
* it. That way, other devices (e.g. a PS/2 or USB mouse) can receive mouse
* events when the bus mouse is disabled. Enabling interrupts constitutes
* enabling the bus mouse. The mouse is considered disabled if interrupts are
* disabled for several consecutive mouse timer ticks; this is because the
* interrupt handler in the guest typically temporarily disables interrupts and
* necessary.
*/
{
}
/**
* Set the emulated hardware to a known initial state.
*/
{
/* Clear the device setup. */
pThis->mouse_buttons = 0;
pThis->mouse_buttons_reported = 0;
pThis->disable_counter = 0;
if (pThis->mouse_enabled)
{
pThis->mouse_enabled = false;
}
}
/* Process a mouse event coming from the host. */
int buttons_state)
{
LogRel3(("%s: dx=%d, dy=%d, dz=%d, dw=%d, buttons_state=0x%x\n",
/* Only record X/Y movement and buttons. */
}
{
/* Toggle the IRQ line if interrupts are enabled. */
{
}
else
{
}
{
if (pThis->disable_counter)
--pThis->disable_counter;
{
pThis->mouse_enabled = false;
}
}
else
{
if (!pThis->mouse_enabled)
{
pThis->mouse_enabled = true;
}
}
/* Re-arm the timer. */
}
# endif /* IN_RING3 */
{
}
/* Update the internal state after a write to port C. */
{
/* If the controller is in hold state, transfer data from counters. */
{
{
/* Force type conversion. */
}
}
else
/* Move the appropriate nibble into port A. */
{
{
else
}
else
{
else
}
/* And update the button bits. */
}
/* Immediately clear the IRQ if necessary. */
{
}
}
{
int rc = VINF_SUCCESS;
switch (offPort)
{
case BMS_PORT_SIG:
/* Update port B. */
break;
case BMS_PORT_DATA:
/* Do nothing, port A is not writable. */
break;
case BMS_PORT_INIT:
break;
case BMS_PORT_CTRL:
/* Update the high nibble of port C. */
break;
default:
break;
}
return rc;
}
{
switch (offPort)
{
case BMS_PORT_DATA:
/* Read port A. */
break;
case BMS_PORT_SIG:
/* Read port B. */
break;
case BMS_PORT_CTRL:
/* Read port C. */
/* Some Microsoft driver code reads the control port 10,000 times when
* determining the IRQ level. This can occur faster than the IRQ line
* transitions and the detection fails. To work around this, we force
* the IRQ bit to toggle every once in a while.
*/
if (pThis->irq_toggle_counter)
else
{
}
break;
case BMS_PORT_INIT:
/* Read the 8255A control port. */
break;
default:
break;
}
return uValue;
}
/**
* Port I/O Handler for port 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) mouIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
{
if (cb == 1)
{
return VINF_SUCCESS;
}
return VERR_IOM_IOPORT_UNUSED;
}
/**
* Port I/O Handler for port 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) mouIOPortWrite(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 the state of the device.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSMHandle The handle to save the state to.
*/
{
/* 8255A state. */
/* Other device state. */
/* Current mouse state deltas. */
/* Timer. */
}
/**
* Loads a saved 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) mouLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t uVersion, uint32_t uPass)
{
int rc;
if (uVersion > BMS_SAVED_STATE_VERSION)
/* 8255A state. */
/* Other device state. */
/* Current mouse state deltas. */
/* Timer. */
return rc;
}
/**
* Reset notification.
*
* @returns VBox status.
* @param pDevIns The device instance data.
*/
{
/* Reinitialize the timer. */
}
/* -=-=-=-=-=- Mouse: IBase -=-=-=-=-=- */
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
{
return NULL;
}
/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
/**
* @interface_method_impl{PDMIMOUSEPORT, pfnPutEvent}
*/
{
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMIMOUSEPORT, pfnPutEventAbs}
*/
static DECLCALLBACK(int) mouPutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t uX, uint32_t uY, int32_t iDeltaZ, int32_t iDeltaW, uint32_t fButtons)
{
}
/* -=-=-=-=-=- setup 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 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 controller doesn't support this action, this is just
* implemented to try out the driver<->device structure.
*/
{
int rc;
("Bus mouse device does not support hotplugging\n"),
switch (iLUN)
{
/* LUN #0: mouse */
case 0:
rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThis->Mouse.IBase, &pThis->Mouse.pDrvBase, "Bus Mouse 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;
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 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: mouse */
case 0:
break;
default:
break;
}
#endif
}
/**
* @copydoc FNPDMDEVRELOCATE
*/
{
}
/**
* @interface_method_impl{PDMDEVREG,pfnConstruct}
*/
{
int rc;
bool fGCEnabled;
bool fR0Enabled;
/*
* Validate and read the configuration.
*/
if (RT_FAILURE(rc))
if (RT_FAILURE(rc))
if (RT_FAILURE(rc))
fGCEnabled = fR0Enabled = false;
/*
* Initialize the interfaces.
*/
/*
* Create the interrupt timer.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Register I/O ports, saved state, and mouse event handlers.
*/
rc = PDMDevHlpIOPortRegister(pDevIns, BMS_IO_BASE, BMS_IO_SIZE, NULL, mouIOPortWrite, mouIOPortRead, NULL, NULL, "Bus Mouse");
if (RT_FAILURE(rc))
return rc;
if (fGCEnabled)
{
rc = PDMDevHlpIOPortRegisterRC(pDevIns, BMS_IO_BASE, BMS_IO_SIZE, 0, "mouIOPortWrite", "mouIOPortRead", NULL, NULL, "Bus Mouse");
if (RT_FAILURE(rc))
return rc;
}
if (fR0Enabled)
{
rc = PDMDevHlpIOPortRegisterR0(pDevIns, BMS_IO_BASE, BMS_IO_SIZE, 0, "mouIOPortWrite", "mouIOPortRead", NULL, NULL, "Bus Mouse");
if (RT_FAILURE(rc))
return rc;
}
rc = PDMDevHlpSSMRegister(pDevIns, BMS_SAVED_STATE_VERSION, sizeof(*pThis), mouSaveExec, mouLoadExec);
if (RT_FAILURE(rc))
return rc;
/*
* Attach to the mouse driver.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Initialize the device state.
*/
return VINF_SUCCESS;
}
/**
* The device registration structure.
*/
const PDMDEVREG g_DeviceBusMouse =
{
/* u32Version */
/* szName */
"busmouse",
/* szRCMod */
"VBoxDDGC.gc",
/* szR0Mod */
"VBoxDDR0.r0",
/* pszDescription */
"Microsoft Bus Mouse controller. "
"LUN #0 is the mouse connector.",
/* fFlags */
/* fClass */
/* cMaxInstances */
1,
/* cbInstance */
sizeof(MouState),
/* pfnConstruct */
/* pfnDestruct */
NULL,
/* 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 */