VBoxMouse.cpp revision eb0398a29ff0e9c8456afeef89fbffca86a626e8
/** @file
*
* VBoxGuest -- VirtualBox Win32 guest mouse filter driver
*
* Based on a Microsoft DDK sample
*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
// #define LOG_ENABLED
#include "VBoxMouse.h"
// VBOX start
#include <VBox/VBoxGuest.h>
#include <VBox/VBoxGuestLib.h>
// VBOX end
#ifdef ALLOC_PRAGMA
#endif
)
/*++
Routine Description:
Initialize the entry points of the driver.
--*/
{
ULONG i;
dprintf(("VBoxMouse::DriverEntry\n"));
//
// Fill in all the dispatch entry points with the pass through function
// and the explicitly fill in the functions we are going to intercept
//
for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
{
}
// ExInitializeFastMutex(&vboxControlMutex);
// DriverObject->MajorFunction [IRP_MJ_DEVICE_CONTROL] = VBoxDispatchIo;
dprintf(("leaving DriverEntry with success\n"));
return STATUS_SUCCESS;
}
#ifdef LOG_ENABLED
{
dprintf ((
"devExt %p\n"
" %p %p %p\n"
" %d %p %p %p %p\n"
" %p\n"
" %p %p\n"
" %d %d %d %d\n"
" %p %d\n",
devExt->EnableCount, devExt->UpperContext, devExt->UpperIsrHook, devExt->IsrWritePort, devExt->CallContext,
));
}
#else
#define dumpDevExt(a) do {} while (0)
#endif /* LOG_ENABLED */
// VBOX start
typedef struct _VBoxGlobalContext
{
volatile LONG cDevicesStarted;
volatile LONG fVBGLInited;
volatile LONG fVBGLInitFailed;
volatile LONG fHostInformed;
volatile LONG fHostMouseFound;
static BOOLEAN vboxIsVBGLInited (void)
{
}
static BOOLEAN vboxIsVBGLInitFailed (void)
{
}
static BOOLEAN vboxIsHostInformed (void)
{
}
static BOOLEAN vboxIsHostMouseFound (void)
{
}
{
if (c == 1)
{
if ( !vboxIsVBGLInited ()
&& !vboxIsVBGLInitFailed ())
{
/* Do one time initialization. */
if (RT_SUCCESS (vboxRC))
{
/* Atomically set the flag. */
dprintf(("VBoxMouse::vboxDeviceAdded: Guest library initialization OK\n"));
}
else
{
dprintf(("VBoxMouse::vboxDeviceAdded: Guest library initialization failed\n"));
}
}
}
if (!vboxIsHostMouseFound ())
{
ResultLength = 0;
sizeof (Property),
&Property,
&ResultLength);
dprintf(("VBoxMouse::vboxDeviceAdded: looking for host mouse: Len is %d, status %x\n",
ResultLength, status));
if (status == STATUS_SUCCESS)
{
/* Check whether the device claims the IO port 0x60 or interrupt 12. */
dprintf(("VBoxMouse::vboxDeviceAdded: Configuration: Number of descriptors = %d\n",
pResourceList->Count));
ULONG iDescriptor = 0;
{
dprintf(("VBoxMouse::vboxDeviceAdded: Descriptor %d: InterfaceType %d, BusType %d, list ver %d, list rev %d, count %d\n",
ULONG iPartialDescriptor = 0;
{
CM_PARTIAL_RESOURCE_DESCRIPTOR *pPartialDescriptor = &pFullDescriptor->PartialResourceList.PartialDescriptors[iPartialDescriptor];
dprintf(("VBoxMouse::vboxDeviceAdded: PartialDescriptor %d: type %d, ShareDisposition %d, Flags 0x%04X, Start 0x%llx, length 0x%x\n",
switch(pPartialDescriptor->Type)
{
case CmResourceTypePort:
dprintf(("VBoxMouse::vboxDeviceAdded: PartialDescriptor %d: Port 0x%x\n", iPartialDescriptor, pPartialDescriptor->u.Port.Start.QuadPart));
{
}
break;
case CmResourceTypeInterrupt:
dprintf(("VBoxMouse::vboxDeviceAdded: PartialDescriptor %d: Int %ld\n", iPartialDescriptor, pPartialDescriptor->u.Interrupt.Vector));
{
}
break;
default:
break;
}
}
}
if (bDetected)
{
* For this device the filter will query absolute mouse coords from the host.
*/
dprintf(("VBoxMouse::vboxDeviceAdded: Host mouse found.\n"));
}
}
}
}
{
dprintf(("VBoxMouse::vboxDeviceRemoved\n"));
/* Save the allocated request pointer and clear the devExt. */
VMMDevReqMouseStatus *reqSC = (VMMDevReqMouseStatus *)InterlockedExchangePointer ((PVOID volatile *)&devExt->reqSC, NULL);
{
// tell the VMM that from now on we can't handle absolute coordinates anymore
int vboxRC = VbglGRAlloc ((VMMDevRequestHeader **)&req, sizeof (VMMDevReqMouseStatus), VMMDevReq_SetMouseStatus);
if (RT_SUCCESS(vboxRC))
{
req->mouseFeatures = 0;
req->pointerXPos = 0;
req->pointerYPos = 0;
{
dprintf(("VBoxMouse::vboxDeviceRemoved: ERROR communicating new mouse capabilities to VMMDev.\n"
}
}
else
{
dprintf(("VBoxMouse::vboxDeviceRemoved: the request allocation has failed.\n"));
}
}
if (reqSC)
{
}
if (c == 0)
{
if (vboxIsVBGLInited ())
{
/* Set the flag to prevent reinitializing of the VBGL. */
VbglTerminate ();
/* The VBGL is now in the not initialized state. */
}
}
}
{
if (vboxIsVBGLInited ())
{
{
int vboxRC = VbglGRAlloc ((VMMDevRequestHeader **)&req, sizeof (VMMDevReqMouseStatus), VMMDevReq_SetMouseStatus);
if (RT_SUCCESS(vboxRC))
{
/* Inform host that we support absolute */
req->pointerXPos = 0;
req->pointerYPos = 0;
{
dprintf(("VBoxMouse::vboxInformHost: ERROR communicating new mouse capabilities to VMMDev."
}
else
{
}
}
}
{
/* Preallocate request for ServiceCallback */
int vboxRC = VbglGRAlloc ((VMMDevRequestHeader **)&req, sizeof (VMMDevReqMouseStatus), VMMDevReq_GetMouseStatus);
if (RT_SUCCESS(vboxRC))
{
dumpDevExt (devExt);
}
else
{
dprintf(("VBoxMouse::vboxInformHost: request allocation for service callback failed\n"));
}
}
}
}
// VBOX end
)
{
PAGED_CODE();
#ifdef LOG_ENABLED
ULONG ResultLength = 0;
wszProperty[0] = 0;
sizeof (wszProperty),
&ResultLength);
if (status == STATUS_SUCCESS)
{
}
#endif /* LOG_ENABLED */
sizeof(DEVICE_EXTENSION),
NULL,
0,
);
if (!NT_SUCCESS(status)) {
return (status);
}
return STATUS_DEVICE_NOT_CONNECTED;
}
// VBOX start
// VBOX end
dprintf(("VBoxMouse::AddDevice: DevExt = %p, returning from AddDevice with rc = 0x%x\n", devExt, status));
return status;
}
)
/*++
Routine Description:
Generic completion routine that allows the driver to send the irp down the
stack, catch it on the way up, and do more processing at the original IRQL.
--*/
{
//
// We could switch on the major and minor functions of the IRP to perform
// different functions, but we know that Context is an event that needs
// to be set.
//
return STATUS_MORE_PROCESSING_REQUIRED;
}
)
/*++
Routine Description:
Maintain a simple count of the creates and closes sent against this device
--*/
{
PAGED_CODE();
switch (irpStack->MajorFunction) {
case IRP_MJ_CREATE:
dprintf(("VBoxMouse::CreateClose: IRP_MJ_CREATE\n"));
//
// No Connection yet. How can we be enabled?
//
dprintf(("VBoxMouse::CreateClose: Not connected, returning STATUS_INVALID_DEVICE_STATE\n"));
}
//
// First time enable here
//
}
else {
//
// More than one create was sent down
//
}
break;
case IRP_MJ_CLOSE:
dprintf(("VBoxMouse::CreateClose: IRP_MJ_CLOSE\n"));
//
// successfully closed the device, do any appropriate work here
//
}
break;
}
//
// Pass on the create and the close
//
}
)
/*++
Routine Description:
Passes a request on to the lower driver.
Considerations:
If you are creating another device object (to communicate with user mode
via IOCTLs), then this function must act differently based on the intended
device object. If the IRP is being sent to the solitary device object, then
this function should just complete the IRP (becuase there is no more stack
locations below it). If the IRP is being sent to the PnP built stack, then
the IRP should be passed down the stack.
These changes must also be propagated to all the other IRP_MJ dispatch
functions (such as create, close, cleanup, etc.) as well!
--*/
{
dprintf(("VBoxMouse_DispatchPassThrough\n"));
//
// Pass the IRP to the target
//
}
)
/*++
Routine Description:
This routine is the dispatch routine for internal device control requests.
There are two specific control codes that are of interest:
IOCTL_INTERNAL_MOUSE_CONNECT:
Store the old context and function pointer and replace it with our own.
This makes life much simpler than intercepting IRPs sent by the RIT and
modifying them on the way back up.
IOCTL_INTERNAL_I8042_HOOK_MOUSE:
Add in the necessary function pointers and context values so that we can
alter how the ps/2 mouse is initialized.
NOTE: Handling IOCTL_INTERNAL_I8042_HOOK_MOUSE is *NOT* necessary if
all you want to do is filter MOUSE_INPUT_DATAs. You can remove
the handling code and all related device extension fields and
functions to conserve space.
Arguments:
DeviceObject - Pointer to the device object.
Irp - Pointer to the request packet.
Return Value:
Status is returned.
--*/
{
dprintf(("VBoxMouse_InternIoCtl: %08X, fn = %d(%04X)\n",
));
//
// Connect a mouse class device driver to the port driver.
//
//
// Only allow one connection.
//
break;
}
sizeof(CONNECT_DATA)) {
//
// invalid buffer
//
break;
}
//
// Copy the connection parameters to the device extension.
//
connectData = ((PCONNECT_DATA)
//
// Hook into the report chain. Everytime a mouse packet is reported to
// the system, VBoxMouse_ServiceCallback will be called
//
break;
//
// Disconnect a mouse class device driver from the port driver.
//
//
// Clear the connection parameters in the device extension.
//
// devExt->UpperConnectData.ClassDeviceObject = NULL;
// devExt->UpperConnectData.ClassService = NULL;
break;
//
// Attach this driver to the initialization and byte processing of the
// i8042 (ie PS/2) mouse. This is only necessary if you want to do PS/2
// specific functions, otherwise hooking the CONNECT_DATA is sufficient
//
sizeof(INTERNAL_I8042_HOOK_MOUSE)) {
//
// invalid buffer
//
break;
}
//
// Copy the connection parameters to the device extension.
//
//
// Set isr routine and context and record any values from above this driver
//
if (hookMouse->IsrRoutine) {
}
//
// Store all of the other functions we might need in the future
//
break;
//
// These internal ioctls are not supported by the new PnP model.
//
#if 0 // obsolete
break;
#endif // obsolete
//
// Might want to capture this in the future. For now, then pass it down
// the stack. These queries must be successful for the RIT to communicate
// with the mouse.
//
default:
break;
}
// VBOX start
dprintf(("VBoxMouse_InternIoCtl: calling VBoxInformHost\n"));
// VBOX end
if (!NT_SUCCESS(status)) {
return status;
}
}
)
/*++
Routine Description:
This routine is the dispatch routine for plug and play irps
Arguments:
DeviceObject - Pointer to the device object.
Irp - Pointer to the request packet.
Return Value:
Status is returned.
--*/
{
PAGED_CODE();
switch (irpStack->MinorFunction) {
case IRP_MN_START_DEVICE: {
dprintf(("VBoxMouse_PnP: IRP_MN_START_DEVICE\n"));
//
// The device is starting.
//
// We cannot touch the device (send it any non pnp irps) until a
// start device has been passed down to the lower drivers.
//
);
&event,
TRUE,
TRUE,
TRUE); // No need for Cancel
if (STATUS_PENDING == status) {
&event,
Executive, // Waiting for reason of a driver
KernelMode, // Waiting in kernel mode
FALSE, // No allert
NULL); // No timeout
}
}
//
// We must now complete the IRP, since we stopped it in the
// completetion routine with MORE_PROCESSING_REQUIRED.
//
break;
}
case IRP_MN_SURPRISE_REMOVAL:
dprintf(("VBoxMouse_PnP: IRP_MN_SURPRISE_REMOVAL\n"));
//
// Same as a remove device, but don't call IoDetach or IoDeleteDevice
//
// Remove code here
break;
case IRP_MN_REMOVE_DEVICE:
{
dprintf(("VBoxMouse_PnP: IRP_MN_REMOVE_DEVICE\n"));
// VBOX start
// VBOX end
// remove code here
} break;
case IRP_MN_QUERY_STOP_DEVICE:
case IRP_MN_STOP_DEVICE:
case IRP_MN_QUERY_INTERFACE:
case IRP_MN_QUERY_DEVICE_TEXT:
case IRP_MN_QUERY_RESOURCES:
case IRP_MN_READ_CONFIG:
case IRP_MN_WRITE_CONFIG:
case IRP_MN_EJECT:
case IRP_MN_SET_LOCK:
case IRP_MN_QUERY_ID:
default:
//
// Here the filter driver might modify the behavior of these IRPS
// Please see PlugPlay documentation for use of these IRPs.
//
break;
}
return status;
}
)
/*++
Routine Description:
This routine is the dispatch routine for power irps Does nothing except
record the state of the device.
Arguments:
DeviceObject - Pointer to the device object.
Irp - Pointer to the request packet.
Return Value:
Status is returned.
--*/
{
PAGED_CODE();
switch (irpStack->MinorFunction) {
case IRP_MN_SET_POWER:
if (powerType == DevicePowerState) {
}
case IRP_MN_QUERY_POWER:
case IRP_MN_WAIT_WAKE:
case IRP_MN_POWER_SEQUENCE:
default:
break;
}
}
)
/*++
Remarks:
i8042prt specific code, if you are writing a packet only filter driver, you
can remove this function
Arguments:
DeviceObject - Our context passed during IOCTL_INTERNAL_I8042_HOOK_MOUSE
CurrentInput - Current input packet being formulated by processing all the
interrupts
CurrentOutput - Current list of bytes being written to the mouse or the
i8042 port.
StatusByte - Byte read from I/O port 60 when the interrupt occurred
DataByte - Byte read from I/O port 64 when the interrupt occurred.
This value can be modified and i8042prt will use this value
if ContinueProcessing is TRUE
ContinueProcessing - If TRUE, i8042prt will proceed with normal processing of
the interrupt. If FALSE, i8042prt will return from the
interrupt after this function returns. Also, if FALSE,
it is this functions responsibilityt to report the input
packet via the function provided in the hook IOCTL or via
queueing a DPC within this driver and calling the
service callback function acquired from the connect IOCTL
Return Value:
Status is returned.
--+*/
{
//dprintf(("VBoxMouse_IsrHook\n"));
if (devExt->UpperIsrHook) {
);
if (!retVal || !(*ContinueProcessing)) {
return retVal;
}
}
return retVal;
}
)
/*++
Routine Description:
Called when there are mouse packets to report to the RIT. You can do
anything you like to the packets. For instance:
o Drop a packet altogether
o Mutate the contents of a packet
o Insert packets into the stream
Arguments:
DeviceObject - Context passed during the connect IOCTL
InputDataStart - First packet to be reported
InputDataEnd - One past the last packet to be reported. Total number of
packets is equal to InputDataEnd - InputDataStart
InputDataConsumed - Set to the total number of packets consumed by the RIT
(via the function pointer we replaced in the connect
IOCTL)
Return Value:
Status is returned.
--*/
{
// VBOX start
// dprintf(("VBoxMouse_ServiceCallback: devExt = %p, reqSC = %p\n", devExt, devExt->reqSC));
if (req)
{
{
{
while (inputData < InputDataEnd)
{
// modify the event data
inputData++;
}
}
}
else
{
dprintf(("VBoxMouse::VBoxMouse_ServiceCallback: ERROR querying mouse capabilities from VMMDev."
}
}
// VBOX end
//
// UpperConnectData must be called at DISPATCH
//
);
}
)
/*++
Routine Description:
Free all the allocated resources associated with this driver.
Arguments:
DriverObject - Pointer to the driver object.
Return Value:
None.
--*/
{
PAGED_CODE();
dprintf(("VBoxMouse_Unload\n"));
}