UsbMouse.cpp revision ae0f2178b9a5aded928e0245cb830ba1d3d04c57
/** @file
* UsbMouse - USB Human Interface Device Emulation (Mouse).
*/
/*
* Copyright (C) 2007-2010 Sun Microsystems, Inc.
*
* Sun Microsystems, Inc. confidential
* All rights reserved
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_USB_MSD
#include <iprt/critsect.h>
#include <iprt/semaphore.h>
#include "../Builtins.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** @name USB HID string IDs
* @{ */
#define USBHID_STR_ID_MANUFACTURER 1
#define USBHID_STR_ID_PRODUCT 2
/** @} */
/** @name USB HID specific descriptor types
* @{ */
#define DT_IF_HID_REPORT 0x22
/** @} */
/** @name USB HID vendor and product IDs
* @{ */
#define VBOX_USB_VENDOR 0x80EE
#define USBHID_PID_MOUSE 0x0020
/** @} */
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The USB HID request state.
*/
typedef enum USBHIDREQSTATE
{
/** Invalid status. */
/** Ready to receive a new read request. */
/** Have (more) data for the host. */
/** Waiting to supply status information to the host. */
/** The end of the valid states. */
/**
* Endpoint status data.
*/
typedef struct USBHIDEP
{
bool fHalted;
} USBHIDEP;
/** Pointer to the endpoint status. */
/**
* A URB queue.
*/
typedef struct USBHIDURBQUEUE
{
/** The head pointer. */
/** Where to insert the next entry. */
/** Pointer to a URB queue. */
typedef USBHIDURBQUEUE *PUSBHIDURBQUEUE;
/** Pointer to a const URB queue. */
typedef USBHIDURBQUEUE const *PCUSBHIDURBQUEUE;
/**
* Mouse movement accumulator.
*/
typedef struct USBHIDM_ACCUM
{
/**
* The USB HID instance data.
*/
typedef struct USBHID
{
/** Pointer back to the PDM USB Device instance structure. */
/** Critical section protecting the device state. */
/** The current configuration.
* (0 - default, 1 - the one supported configuration, i.e configured.) */
/** Endpoint 0 is the default control pipe, 1 is the dev->host interrupt one. */
/** The state of the HID (state machine).*/
/** Pointer movement accumulator. */
/** Pending to-host queue.
* The URBs waiting here are waiting for data to become available.
*/
/** Done queue
* The URBs stashed here are waiting to be reaped. */
/** Signalled when adding an URB to the done queue and fHaveDoneQueueWaiter
* is set. */
/** Someone is waiting on the done queue. */
bool fHaveDoneQueueWaiter;
/**
* 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. */
} Lun0;
} USBHID;
/** Pointer to the USB HID instance data. */
/**
* The USB HID report structure.
*/
typedef struct USBHIDM_REPORT
{
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static const PDMUSBDESCCACHESTRING g_aUsbHidStrings_en_US[] =
{
{ USBHID_STR_ID_MANUFACTURER, "VirtualBox" },
{ USBHID_STR_ID_PRODUCT, "USB Mouse" },
};
static const PDMUSBDESCCACHELANG g_aUsbHidLanguages[] =
{
};
static const VUSBDESCENDPOINTEX g_aUsbHidEndpointDescs[] =
{
{
{
/* .bLength = */ sizeof(VUSBDESCENDPOINT),
/* .bDescriptorType = */ VUSB_DT_ENDPOINT,
/* .bEndpointAddress = */ 0x81 /* ep=1, in */,
/* .bmAttributes = */ 3 /* interrupt */,
/* .wMaxPacketSize = */ 4,
/* .bInterval = */ 10,
},
/* .pvMore = */ NULL,
/* .pvClass = */ NULL,
/* .cbClass = */ 0
},
};
/* HID report descriptor. */
static const uint8_t g_UsbHidReportDesc[] =
{
/* Usage Page */ 0x05, 0x01, /* Generic Desktop */
/* Usage */ 0x09, 0x02, /* Mouse */
/* Collection */ 0xA1, 0x01, /* Application */
/* Usage */ 0x09, 0x01, /* Pointer */
/* Collection */ 0xA1, 0x00, /* Physical */
/* Usage Page */ 0x05, 0x09, /* Button */
/* Usage Minimum */ 0x19, 0x01, /* Button 1 */
/* Usage Maximum */ 0x29, 0x03, /* Button 3 */
/* Logical Minimum */ 0x15, 0x00, /* 0 */
/* Logical Maximum */ 0x25, 0x01, /* 1 */
/* Report Count */ 0x95, 0x03, /* 3 */
/* Report Size */ 0x75, 0x01, /* 1 */
/* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
/* Report Count */ 0x95, 0x01, /* 1 */
/* Report Size */ 0x75, 0x05, /* 5 (padding bits) */
/* Input */ 0x81, 0x03, /* Constant, Value, Absolute, Bit field */
/* Usage Page */ 0x05, 0x01, /* Generic Desktop */
/* Usage */ 0x09, 0x30, /* X */
/* Usage */ 0x09, 0x31, /* Y */
/* Usage */ 0x09, 0x38, /* Z (wheel) */
/* Logical Minimum */ 0x15, 0x81, /* -127 */
/* Logical Maximum */ 0x25, 0x7F, /* +127 */
/* Report Size */ 0x75, 0x08, /* 8 */
/* Report Count */ 0x95, 0x03, /* 3 */
/* Input */ 0x81, 0x06, /* Data, Value, Relative, Bit field */
/* End Collection */ 0xC0,
/* End Collection */ 0xC0,
};
/* Additional HID class interface descriptor. */
static const uint8_t g_UsbHidIfHidDesc[] =
{
/* .bLength = */ 0x09,
/* .bDescriptorType = */ 0x21, /* HID */
/* .bcdHID = */ 0x10, 0x01, /* 1.1 */
/* .bCountryCode = */ 0,
/* .bNumDescriptors = */ 1,
/* .bDescriptorType = */ 0x22, /* Report */
};
static const VUSBDESCINTERFACEEX g_UsbHidInterfaceDesc =
{
{
/* .bLength = */ sizeof(VUSBDESCINTERFACE),
/* .bDescriptorType = */ VUSB_DT_INTERFACE,
/* .bInterfaceNumber = */ 0,
/* .bAlternateSetting = */ 0,
/* .bNumEndpoints = */ 1,
/* .bInterfaceClass = */ 3 /* HID */,
/* .bInterfaceSubClass = */ 1 /* Boot Interface */,
/* .bInterfaceProtocol = */ 2 /* Mouse */,
/* .iInterface = */ 0
},
/* .pvMore = */ NULL,
/* .pvClass = */ &g_UsbHidIfHidDesc,
/* .cbClass = */ sizeof(g_UsbHidIfHidDesc),
};
static const VUSBINTERFACE g_aUsbHidInterfaces[] =
{
};
static const VUSBDESCCONFIGEX g_UsbHidConfigDesc =
{
{
/* .bLength = */ sizeof(VUSBDESCCONFIG),
/* .bDescriptorType = */ VUSB_DT_CONFIG,
/* .wTotalLength = */ 0 /* recalculated on read */,
/* .bConfigurationValue =*/ 1,
/* .iConfiguration = */ 0,
/* .MaxPower = */ 50 /* 100mA */
},
NULL,
};
static const VUSBDESCDEVICE g_UsbHidDeviceDesc =
{
/* .bLength = */ sizeof(g_UsbHidDeviceDesc),
/* .bDescriptorType = */ VUSB_DT_DEVICE,
/* .bcdUsb = */ 0x110, /* 1.1 */
/* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
/* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
/* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
/* .bMaxPacketSize0 = */ 8,
/* .idVendor = */ VBOX_USB_VENDOR,
/* .idProduct = */ USBHID_PID_MOUSE,
/* .bcdDevice = */ 0x0100, /* 1.0 */
/* .iManufacturer = */ USBHID_STR_ID_MANUFACTURER,
/* .iProduct = */ USBHID_STR_ID_PRODUCT,
/* .iSerialNumber = */ 0,
/* .bNumConfigurations = */ 1
};
static const PDMUSBDESCCACHE g_UsbHidDescCache =
{
/* .pDevice = */ &g_UsbHidDeviceDesc,
/* .paConfigs = */ &g_UsbHidConfigDesc,
/* .paLanguages = */ g_aUsbHidLanguages,
/* .fUseCachedDescriptors = */ true,
/* .fUseCachedStringsDescriptors = */ true
};
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Initializes an URB queue.
*
* @param pQueue The URB queue.
*/
{
}
/**
* Inserts an URB at the end of the queue.
*
* @param pQueue The URB queue.
* @param pUrb The URB to insert.
*/
{
}
/**
* Unlinks the head of the queue and returns it.
*
* @returns The head entry.
* @param pQueue The URB queue.
*/
{
if (pUrb)
{
if (!pNext)
else
}
return pUrb;
}
/**
* Removes an URB from anywhere in the queue.
*
* @returns true if found, false if not.
* @param pQueue The URB queue.
* @param pUrb The URB to remove.
*/
{
else
{
while (pCur)
{
{
break;
}
}
if (!pCur)
return false;
}
return true;
}
/**
* Checks if the queue is empty or not.
*
* @returns true if it is, false if it isn't.
* @param pQueue The URB queue.
*/
{
}
/**
* Links an URB into the done queue.
*
* @param pThis The HID instance.
* @param pUrb The URB.
*/
{
if (pThis->fHaveDoneQueueWaiter)
{
}
}
/**
* Completes the URB with a stalled state, halting the pipe.
*/
{
Log(("usbHidCompleteStall/#%u: pUrb=%p:%s: %s\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, pszWhy));
/** @todo figure out if the stall is global or pipe-specific or both. */
if (pEp)
else
{
}
return VINF_SUCCESS;
}
/**
* Completes the URB with a OK state.
*/
{
Log(("usbHidCompleteOk/#%u: pUrb=%p:%s cbData=%#zx\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, cbData));
return VINF_SUCCESS;
}
/**
* Reset worker for usbHidUsbReset, usbHidUsbSetConfiguration and
* usbHidUrbHandleDefaultPipe.
*
* @returns VBox status code.
* @param pThis The HID instance.
* @param pUrb Set when usbHidUrbHandleDefaultPipe is the
* caller.
* @param fSetConfig Set when usbHidUsbSetConfiguration is the
* caller.
*/
{
/*
* Wait for the any command currently executing to complete before
* resetting. (We cannot cancel its execution.) How we do this depends
* on the reset method.
*/
/*
* Reset the device state.
*/
/*
* Ditch all pending URBs.
*/
{
}
if (pUrb)
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
{
return NULL;
}
{
if (val > 127) {
val = 127;
} else if (val < -127) {
val = -127;
}
return val;
}
/**
* Mouse event handler.
*
* @returns VBox status code.
* @param pInterface Pointer to the mouse port interface (KBDState::Mouse.iPort).
* @param i32DeltaX The X delta.
* @param i32DeltaY The Y delta.
* @param i32DeltaZ The Z delta.
* @param i32DeltaW The W delta.
* @param fButtonStates The button states.
*/
static DECLCALLBACK(int) usbHidMousePutEvent(PPDMIMOUSEPORT pInterface, int32_t i32DeltaX, int32_t i32DeltaY, int32_t i32DeltaZ, int32_t i32DeltaW, uint32_t fButtonStates)
{
// int rc = PDMCritSectEnter(&pThis->CritSect, VERR_SEM_BUSY);
// AssertReleaseRC(rc);
/* Accumulate movement - the events from the front end may arrive
* at a much higher rate than USB can handle.
*/
/* Check if there's a URB waiting. If so, send a report.
*/
if (pUrb)
{
/* Clear the accumulated movement. */
}
// PDMCritSectLeave(&pThis->CritSect);
return VINF_SUCCESS;
}
/**
* @copydoc PDMUSBREG::pfnUrbReap
*/
{
{
/* Wait */
pThis->fHaveDoneQueueWaiter = true;
pThis->fHaveDoneQueueWaiter = false;
}
if (pUrb)
return pUrb;
}
/**
* @copydoc PDMUSBREG::pfnUrbCancel
*/
{
/*
* Remove the URB from the to-host queue and move it onto the done queue.
*/
return VINF_SUCCESS;
}
/**
* Handles request sent to the inbound (device to host) interrupt pipe. This is
* rather different from bulk requests because an interrupt read URB may complete
* after arbitrarily long time.
*/
{
/*
* Stall the request if the pipe is halted.
*/
/*
* Deal with the URB according to the state.
*/
{
/*
* We've data left to transfer to the host.
*/
{
AssertFailed();
Log(("usbHidHandleIntrDevToHost: Entering STATUS\n"));
}
/*
* Status transfer.
*/
case USBHIDREQSTATE_STATUS:
{
AssertFailed();
Log(("usbHidHandleIntrDevToHost: Entering READY\n"));
}
case USBHIDREQSTATE_READY:
return VINF_SUCCESS;
/*
* Bad states, stall.
*/
default:
}
}
/**
* Handles request sent to the default control pipe.
*/
{
{
{
case VUSB_REQ_GET_DESCRIPTOR:
{
switch (pSetup->bmRequestType)
{
{
{
case VUSB_DT_STRING:
break;
default:
break;
}
break;
}
{
{
case DT_IF_HID_REPORT:
/* Returned data is written after the setup message. */
Log(("usbHid: GET_DESCRIPTOR DT_IF_HID_REPORT wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
default:
break;
}
break;
}
default:
}
break;
}
case VUSB_REQ_CLEAR_FEATURE:
break;
}
/** @todo implement this. */
Log(("usbHid: Implement standard request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
}
/* 3.1 Bulk-Only Mass Storage Reset */
{
Log(("usbHidHandleDefaultPipe: Bulk-Only Mass Storage Reset\n"));
}
else
{
Log(("usbHid: Unknown control msg: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
}
return VINF_SUCCESS;
}
/**
* @copydoc PDMUSBREG::pfnQueue
*/
{
LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
/*
* Parse on a per end-point basis.
*/
int rc;
{
case 0:
break;
case 0x81:
AssertFailed();
case 0x01:
break;
default:
break;
}
return rc;
}
/**
* @copydoc PDMUSBREG::pfnUsbClearHaltedEndpoint
*/
{
{
}
return VINF_SUCCESS;
}
/**
* @copydoc PDMUSBREG::pfnUsbSetInterface
*/
static DECLCALLBACK(int) usbHidUsbSetInterface(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
{
LogFlow(("usbHidUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n", pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
Assert(bAlternateSetting == 0);
return VINF_SUCCESS;
}
/**
* @copydoc PDMUSBREG::pfnUsbSetConfiguration
*/
{
LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
/*
* If the same config is applied more than once, it's a kind of reset.
*/
return VINF_SUCCESS;
}
/**
* @copydoc PDMUSBREG::pfnUsbGetDescriptorCache
*/
{
return &g_UsbHidDescCache;
}
/**
* @copydoc PDMUSBREG::pfnUsbReset
*/
{
return rc;
}
/**
* @copydoc PDMUSBREG::pfnDestruct
*/
{
{
}
{
}
}
/**
* @copydoc PDMUSBREG::pfnConstruct
*/
static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
{
/*
* Perform the basic structure initialization first so the destructor
* will not misbehave.
*/
/*
* Validate and read the configuration.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Attach the mouse driver.
*/
rc = pUsbIns->pHlpR3->pfnDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "Mouse Port");
if (RT_FAILURE(rc))
return VINF_SUCCESS;
}
/**
* The USB Human Interface Device (HID) Mouse registration record.
*/
const PDMUSBREG g_UsbHidMou =
{
/* u32Version */
/* szName */
"HidMouse",
/* pszDescription */
"USB HID Mouse.",
/* fFlags */
0,
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(USBHID),
/* pfnConstruct */
/* pfnDestruct */
/* pfnVMInitComplete */
NULL,
/* pfnVMPowerOn */
NULL,
/* pfnVMReset */
NULL,
/* pfnVMSuspend */
NULL,
/* pfnVMResume */
NULL,
/* pfnVMPowerOff */
NULL,
/* pfnHotPlugged */
NULL,
/* pfnHotUnplugged */
NULL,
/* pfnDriverAttach */
NULL,
/* pfnDriverDetach */
NULL,
/* pfnQueryInterface */
NULL,
/* pfnUsbReset */
/* pfnUsbGetCachedDescriptors */
/* pfnUsbSetConfiguration */
/* pfnUsbSetInterface */
/* pfnUsbClearHaltedEndpoint */
/* pfnUrbNew */
NULL/*usbHidUrbNew*/,
/* pfnQueue */
/* pfnUrbCancel */
/* pfnUrbReap */
/* u32TheEnd */
};