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 <VBox/pdmusb.h>
#include <VBox/log.h>
#include <VBox/err.h>
#include <iprt/assert.h>
#include <iprt/critsect.h>
#include <iprt/mem.h>
#include <iprt/semaphore.h>
#include <iprt/string.h>
#include <iprt/uuid.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. */
USBHIDREQSTATE_INVALID = 0,
/** Ready to receive a new read request. */
USBHIDREQSTATE_READY,
/** Have (more) data for the host. */
USBHIDREQSTATE_DATA_TO_HOST,
/** Waiting to supply status information to the host. */
USBHIDREQSTATE_STATUS,
/** The end of the valid states. */
USBHIDREQSTATE_END
} USBHIDREQSTATE;
/**
* Endpoint status data.
*/
typedef struct USBHIDEP
{
bool fHalted;
} USBHIDEP;
/** Pointer to the endpoint status. */
typedef USBHIDEP *PUSBHIDEP;
/**
* A URB queue.
*/
typedef struct USBHIDURBQUEUE
{
/** The head pointer. */
PVUSBURB pHead;
/** Where to insert the next entry. */
PVUSBURB *ppTail;
} USBHIDURBQUEUE;
/** 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
{
uint32_t btn;
int32_t dX;
int32_t dY;
int32_t dZ;
} USBHIDM_ACCUM, *PUSBHIDM_ACCUM;
/**
* The USB HID instance data.
*/
typedef struct USBHID
{
/** Pointer back to the PDM USB Device instance structure. */
PPDMUSBINS pUsbIns;
/** Critical section protecting the device state. */
RTCRITSECT CritSect;
/** The current configuration.
* (0 - default, 1 - the one supported configuration, i.e configured.) */
uint8_t bConfigurationValue;
/** Endpoint 0 is the default control pipe, 1 is the dev->host interrupt one. */
USBHIDEP aEps[2];
/** The state of the HID (state machine).*/
USBHIDREQSTATE enmState;
/** Pointer movement accumulator. */
USBHIDM_ACCUM PtrDelta;
/** Pending to-host queue.
* The URBs waiting here are waiting for data to become available.
*/
USBHIDURBQUEUE ToHostQueue;
/** Done queue
* The URBs stashed here are waiting to be reaped. */
USBHIDURBQUEUE DoneQueue;
/** Signalled when adding an URB to the done queue and fHaveDoneQueueWaiter
* is set. */
RTSEMEVENT hEvtDoneQueue;
/** 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. */
PDMIBASE IBase;
/** The mouse port base interface. */
PDMIMOUSEPORT IPort;
/** The base interface of the attached mouse driver. */
R3PTRTYPE(PPDMIBASE) pDrvBase;
/** The mouse interface of the attached mouse driver. */
R3PTRTYPE(PPDMIMOUSECONNECTOR) pDrv;
} Lun0;
} USBHID;
/** Pointer to the USB HID instance data. */
typedef USBHID *PUSBHID;
/**
* The USB HID report structure.
*/
typedef struct USBHIDM_REPORT
{
uint8_t btn;
int8_t dx;
int8_t dy;
int8_t dz;
} USBHIDM_REPORT, *PUSBHIDM_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[] =
{
{ 0x0409, RT_ELEMENTS(g_aUsbHidStrings_en_US), g_aUsbHidStrings_en_US }
};
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 */
/* .wDescriptorLength = */ sizeof(g_UsbHidReportDesc), 0x00
};
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),
&g_aUsbHidEndpointDescs[0]
};
static const VUSBINTERFACE g_aUsbHidInterfaces[] =
{
{ &g_UsbHidInterfaceDesc, /* .cSettings = */ 1 },
};
static const VUSBDESCCONFIGEX g_UsbHidConfigDesc =
{
{
/* .bLength = */ sizeof(VUSBDESCCONFIG),
/* .bDescriptorType = */ VUSB_DT_CONFIG,
/* .wTotalLength = */ 0 /* recalculated on read */,
/* .bNumInterfaces = */ RT_ELEMENTS(g_aUsbHidInterfaces),
/* .bConfigurationValue =*/ 1,
/* .iConfiguration = */ 0,
/* .bmAttributes = */ RT_BIT(7),
/* .MaxPower = */ 50 /* 100mA */
},
NULL,
&g_aUsbHidInterfaces[0]
};
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,
/* .cLanguages = */ RT_ELEMENTS(g_aUsbHidLanguages),
/* .fUseCachedDescriptors = */ true,
/* .fUseCachedStringsDescriptors = */ true
};
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Initializes an URB queue.
*
* @param pQueue The URB queue.
*/
static void usbHidQueueInit(PUSBHIDURBQUEUE pQueue)
{
pQueue->pHead = NULL;
pQueue->ppTail = &pQueue->pHead;
}
/**
* Inserts an URB at the end of the queue.
*
* @param pQueue The URB queue.
* @param pUrb The URB to insert.
*/
DECLINLINE(void) usbHidQueueAddTail(PUSBHIDURBQUEUE pQueue, PVUSBURB pUrb)
{
pUrb->Dev.pNext = NULL;
*pQueue->ppTail = pUrb;
pQueue->ppTail = &pUrb->Dev.pNext;
}
/**
* Unlinks the head of the queue and returns it.
*
* @returns The head entry.
* @param pQueue The URB queue.
*/
DECLINLINE(PVUSBURB) usbHidQueueRemoveHead(PUSBHIDURBQUEUE pQueue)
{
PVUSBURB pUrb = pQueue->pHead;
if (pUrb)
{
PVUSBURB pNext = pUrb->Dev.pNext;
pQueue->pHead = pNext;
if (!pNext)
pQueue->ppTail = &pQueue->pHead;
else
pUrb->Dev.pNext = NULL;
}
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.
*/
DECLINLINE(bool) usbHidQueueRemove(PUSBHIDURBQUEUE pQueue, PVUSBURB pUrb)
{
PVUSBURB pCur = pQueue->pHead;
if (pCur == pUrb)
pQueue->pHead = pUrb->Dev.pNext;
else
{
while (pCur)
{
if (pCur->Dev.pNext == pUrb)
{
pCur->Dev.pNext = pUrb->Dev.pNext;
break;
}
pCur = pCur->Dev.pNext;
}
if (!pCur)
return false;
}
if (!pUrb->Dev.pNext)
pQueue->ppTail = &pQueue->pHead;
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.
*/
DECLINLINE(bool) usbHidQueueIsEmpty(PCUSBHIDURBQUEUE pQueue)
{
return pQueue->pHead == NULL;
}
/**
* Links an URB into the done queue.
*
* @param pThis The HID instance.
* @param pUrb The URB.
*/
static void usbHidLinkDone(PUSBHID pThis, PVUSBURB pUrb)
{
usbHidQueueAddTail(&pThis->DoneQueue, pUrb);
if (pThis->fHaveDoneQueueWaiter)
{
int rc = RTSemEventSignal(pThis->hEvtDoneQueue);
AssertRC(rc);
}
}
/**
* Completes the URB with a stalled state, halting the pipe.
*/
static int usbHidCompleteStall(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb, const char *pszWhy)
{
Log(("usbHidCompleteStall/#%u: pUrb=%p:%s: %s\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, pszWhy));
pUrb->enmStatus = VUSBSTATUS_STALL;
/** @todo figure out if the stall is global or pipe-specific or both. */
if (pEp)
pEp->fHalted = true;
else
{
pThis->aEps[1].fHalted = true;
pThis->aEps[2].fHalted = true;
}
usbHidLinkDone(pThis, pUrb);
return VINF_SUCCESS;
}
/**
* Completes the URB with a OK state.
*/
static int usbHidCompleteOk(PUSBHID pThis, PVUSBURB pUrb, size_t cbData)
{
Log(("usbHidCompleteOk/#%u: pUrb=%p:%s cbData=%#zx\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, cbData));
pUrb->enmStatus = VUSBSTATUS_OK;
pUrb->cbData = cbData;
usbHidLinkDone(pThis, pUrb);
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.
*/
static int usbHidResetWorker(PUSBHID pThis, PVUSBURB pUrb, bool fSetConfig)
{
/*
* 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.
*/
pThis->enmState = USBHIDREQSTATE_READY;
for (unsigned i = 0; i < RT_ELEMENTS(pThis->aEps); i++)
pThis->aEps[i].fHalted = false;
if (!pUrb && !fSetConfig) /* (only device reset) */
pThis->bConfigurationValue = 0; /* default */
/*
* Ditch all pending URBs.
*/
PVUSBURB pCurUrb;
while ((pCurUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue)) != NULL)
{
pCurUrb->enmStatus = VUSBSTATUS_CRC;
usbHidLinkDone(pThis, pCurUrb);
}
if (pUrb)
return usbHidCompleteOk(pThis, pUrb, 0);
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
static DECLCALLBACK(void *) usbHidMouseQueryInterface(PPDMIBASE pInterface, const char *pszIID)
{
PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IBase);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Lun0.IBase);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThis->Lun0.IPort);
return NULL;
}
static int8_t clamp_i8(int32_t val)
{
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)
{
PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
// 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.
*/
pThis->PtrDelta.btn = fButtonStates;
pThis->PtrDelta.dX += i32DeltaX;
pThis->PtrDelta.dY += i32DeltaY;
pThis->PtrDelta.dZ -= i32DeltaZ; /* Inverted! */
/* Check if there's a URB waiting. If so, send a report.
*/
PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue);
if (pUrb)
{
USBHIDM_REPORT report;
size_t cbCopy;
//@todo: fix/extend
report.btn = pThis->PtrDelta.btn;
report.dx = clamp_i8(pThis->PtrDelta.dX);
report.dy = clamp_i8(pThis->PtrDelta.dY);
report.dz = clamp_i8(pThis->PtrDelta.dZ);
cbCopy = sizeof(report);
memcpy(&pUrb->abData[0], &report, cbCopy);
usbHidCompleteOk(pThis, pUrb, cbCopy);
/* Clear the accumulated movement. */
pThis->PtrDelta.dX = pThis->PtrDelta.dY = pThis->PtrDelta.dZ = 0;
}
// PDMCritSectLeave(&pThis->CritSect);
return VINF_SUCCESS;
}
/**
* @copydoc PDMUSBREG::pfnUrbReap
*/
static DECLCALLBACK(PVUSBURB) usbHidUrbReap(PPDMUSBINS pUsbIns, RTMSINTERVAL cMillies)
{
PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
LogFlow(("usbHidUrbReap/#%u: cMillies=%u\n", pUsbIns->iInstance, cMillies));
RTCritSectEnter(&pThis->CritSect);
PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
if (!pUrb && cMillies)
{
/* Wait */
pThis->fHaveDoneQueueWaiter = true;
RTCritSectLeave(&pThis->CritSect);
RTSemEventWait(pThis->hEvtDoneQueue, cMillies);
RTCritSectEnter(&pThis->CritSect);
pThis->fHaveDoneQueueWaiter = false;
pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
}
RTCritSectLeave(&pThis->CritSect);
if (pUrb)
Log(("usbHidUrbReap/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
return pUrb;
}
/**
* @copydoc PDMUSBREG::pfnUrbCancel
*/
static DECLCALLBACK(int) usbHidUrbCancel(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
{
PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
LogFlow(("usbHidUrbCancel/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
RTCritSectEnter(&pThis->CritSect);
/*
* Remove the URB from the to-host queue and move it onto the done queue.
*/
if (usbHidQueueRemove(&pThis->ToHostQueue, pUrb))
usbHidLinkDone(pThis, pUrb);
RTCritSectLeave(&pThis->CritSect);
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.
*/
static int usbHidHandleIntrDevToHost(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
{
/*
* Stall the request if the pipe is halted.
*/
if (RT_UNLIKELY(pEp->fHalted))
return usbHidCompleteStall(pThis, NULL, pUrb, "Halted pipe");
/*
* Deal with the URB according to the state.
*/
switch (pThis->enmState)
{
/*
* We've data left to transfer to the host.
*/
case USBHIDREQSTATE_DATA_TO_HOST:
{
AssertFailed();
Log(("usbHidHandleIntrDevToHost: Entering STATUS\n"));
return usbHidCompleteOk(pThis, pUrb, 0);
}
/*
* Status transfer.
*/
case USBHIDREQSTATE_STATUS:
{
AssertFailed();
Log(("usbHidHandleIntrDevToHost: Entering READY\n"));
pThis->enmState = USBHIDREQSTATE_READY;
return usbHidCompleteOk(pThis, pUrb, 0);
}
case USBHIDREQSTATE_READY:
usbHidQueueAddTail(&pThis->ToHostQueue, pUrb);
LogFlow(("usbHidHandleIntrDevToHost: Added %p:%s to the queue\n", pUrb, pUrb->pszDesc));
return VINF_SUCCESS;
/*
* Bad states, stall.
*/
default:
Log(("usbHidHandleIntrDevToHost: enmState=%d cbData=%#x\n", pThis->enmState, pUrb->cbData));
return usbHidCompleteStall(pThis, NULL, pUrb, "Really bad state (D2H)!");
}
}
/**
* Handles request sent to the default control pipe.
*/
static int usbHidHandleDefaultPipe(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
{
PVUSBSETUP pSetup = (PVUSBSETUP)&pUrb->abData[0];
AssertReturn(pUrb->cbData >= sizeof(*pSetup), VERR_VUSB_FAILED_TO_QUEUE_URB);
if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_STANDARD)
{
switch (pSetup->bRequest)
{
case VUSB_REQ_GET_DESCRIPTOR:
{
switch (pSetup->bmRequestType)
{
case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
{
switch (pSetup->wValue >> 8)
{
case VUSB_DT_STRING:
Log(("usbHid: GET_DESCRIPTOR DT_STRING wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
break;
default:
Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
break;
}
break;
}
case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
{
switch (pSetup->wValue >> 8)
{
case DT_IF_HID_REPORT:
uint32_t cbCopy;
/* Returned data is written after the setup message. */
cbCopy = pUrb->cbData - sizeof(*pSetup);
cbCopy = RT_MIN(cbCopy, sizeof(g_UsbHidReportDesc));
Log(("usbHid: GET_DESCRIPTOR DT_IF_HID_REPORT wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
memcpy(&pUrb->abData[sizeof(*pSetup)], &g_UsbHidReportDesc, cbCopy);
return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
default:
Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
break;
}
break;
}
default:
Log(("usbHid: Bad GET_DESCRIPTOR req: bmRequestType=%#x\n", pSetup->bmRequestType));
return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_DESCRIPTOR");
}
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",
pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
usbHidCompleteStall(pThis, pEp, pUrb, "TODO: standard request stuff");
}
/* 3.1 Bulk-Only Mass Storage Reset */
else if ( pSetup->bmRequestType == (VUSB_REQ_CLASS | VUSB_TO_INTERFACE)
&& pSetup->bRequest == 0xff
&& !pSetup->wValue
&& !pSetup->wLength
&& pSetup->wIndex == 0)
{
Log(("usbHidHandleDefaultPipe: Bulk-Only Mass Storage Reset\n"));
return usbHidResetWorker(pThis, pUrb, false /*fSetConfig*/);
}
else
{
Log(("usbHid: Unknown control msg: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
return usbHidCompleteStall(pThis, pEp, pUrb, "Unknown control msg");
}
return VINF_SUCCESS;
}
/**
* @copydoc PDMUSBREG::pfnQueue
*/
static DECLCALLBACK(int) usbHidQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
{
PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
RTCritSectEnter(&pThis->CritSect);
/*
* Parse on a per end-point basis.
*/
int rc;
switch (pUrb->EndPt)
{
case 0:
rc = usbHidHandleDefaultPipe(pThis, &pThis->aEps[0], pUrb);
break;
case 0x81:
AssertFailed();
case 0x01:
rc = usbHidHandleIntrDevToHost(pThis, &pThis->aEps[1], pUrb);
break;
default:
AssertMsgFailed(("EndPt=%d\n", pUrb->EndPt));
rc = VERR_VUSB_FAILED_TO_QUEUE_URB;
break;
}
RTCritSectLeave(&pThis->CritSect);
return rc;
}
/**
* @copydoc PDMUSBREG::pfnUsbClearHaltedEndpoint
*/
static DECLCALLBACK(int) usbHidUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns, unsigned uEndpoint)
{
PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
LogFlow(("usbHidUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n", pUsbIns->iInstance, uEndpoint));
if ((uEndpoint & ~0x80) < RT_ELEMENTS(pThis->aEps))
{
RTCritSectEnter(&pThis->CritSect);
pThis->aEps[(uEndpoint & ~0x80)].fHalted = false;
RTCritSectLeave(&pThis->CritSect);
}
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
*/
static DECLCALLBACK(int) usbHidUsbSetConfiguration(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc)
{
PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
Assert(bConfigurationValue == 1);
RTCritSectEnter(&pThis->CritSect);
/*
* If the same config is applied more than once, it's a kind of reset.
*/
if (pThis->bConfigurationValue == bConfigurationValue)
usbHidResetWorker(pThis, NULL, true /*fSetConfig*/); /** @todo figure out the exact difference */
pThis->bConfigurationValue = bConfigurationValue;
RTCritSectLeave(&pThis->CritSect);
return VINF_SUCCESS;
}
/**
* @copydoc PDMUSBREG::pfnUsbGetDescriptorCache
*/
static DECLCALLBACK(PCPDMUSBDESCCACHE) usbHidUsbGetDescriptorCache(PPDMUSBINS pUsbIns)
{
PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
LogFlow(("usbHidUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
return &g_UsbHidDescCache;
}
/**
* @copydoc PDMUSBREG::pfnUsbReset
*/
static DECLCALLBACK(int) usbHidUsbReset(PPDMUSBINS pUsbIns, bool fResetOnLinux)
{
PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
LogFlow(("usbHidUsbReset/#%u:\n", pUsbIns->iInstance));
RTCritSectEnter(&pThis->CritSect);
int rc = usbHidResetWorker(pThis, NULL, false /*fSetConfig*/);
RTCritSectLeave(&pThis->CritSect);
return rc;
}
/**
* @copydoc PDMUSBREG::pfnDestruct
*/
static void usbHidDestruct(PPDMUSBINS pUsbIns)
{
PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
LogFlow(("usbHidDestruct/#%u:\n", pUsbIns->iInstance));
if (RTCritSectIsInitialized(&pThis->CritSect))
{
RTCritSectEnter(&pThis->CritSect);
RTCritSectLeave(&pThis->CritSect);
RTCritSectDelete(&pThis->CritSect);
}
if (pThis->hEvtDoneQueue != NIL_RTSEMEVENT)
{
RTSemEventDestroy(pThis->hEvtDoneQueue);
pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
}
}
/**
* @copydoc PDMUSBREG::pfnConstruct
*/
static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
{
PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
Log(("usbHidConstruct/#%u:\n", iInstance));
/*
* Perform the basic structure initialization first so the destructor
* will not misbehave.
*/
pThis->pUsbIns = pUsbIns;
pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
usbHidQueueInit(&pThis->ToHostQueue);
usbHidQueueInit(&pThis->DoneQueue);
int rc = RTCritSectInit(&pThis->CritSect);
AssertRCReturn(rc, rc);
rc = RTSemEventCreate(&pThis->hEvtDoneQueue);
AssertRCReturn(rc, rc);
/*
* Validate and read the configuration.
*/
rc = CFGMR3ValidateConfig(pCfg, "/", "", "", "UsbHid", iInstance);
if (RT_FAILURE(rc))
return rc;
pThis->Lun0.IBase.pfnQueryInterface = usbHidMouseQueryInterface;
pThis->Lun0.IPort.pfnPutEvent = usbHidMousePutEvent;
/*
* Attach the mouse driver.
*/
rc = pUsbIns->pHlpR3->pfnDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "Mouse Port");
if (RT_FAILURE(rc))
return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to attach mouse driver"));
return VINF_SUCCESS;
}
/**
* The USB Human Interface Device (HID) Mouse registration record.
*/
const PDMUSBREG g_UsbHidMou =
{
/* u32Version */
PDM_USBREG_VERSION,
/* szName */
"HidMouse",
/* pszDescription */
"USB HID Mouse.",
/* fFlags */
0,
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(USBHID),
/* pfnConstruct */
usbHidConstruct,
/* pfnDestruct */
usbHidDestruct,
/* pfnVMInitComplete */
NULL,
/* pfnVMPowerOn */
NULL,
/* pfnVMReset */
NULL,
/* pfnVMSuspend */
NULL,
/* pfnVMResume */
NULL,
/* pfnVMPowerOff */
NULL,
/* pfnHotPlugged */
NULL,
/* pfnHotUnplugged */
NULL,
/* pfnDriverAttach */
NULL,
/* pfnDriverDetach */
NULL,
/* pfnQueryInterface */
NULL,
/* pfnUsbReset */
usbHidUsbReset,
/* pfnUsbGetCachedDescriptors */
usbHidUsbGetDescriptorCache,
/* pfnUsbSetConfiguration */
usbHidUsbSetConfiguration,
/* pfnUsbSetInterface */
usbHidUsbSetInterface,
/* pfnUsbClearHaltedEndpoint */
usbHidUsbClearHaltedEndpoint,
/* pfnUrbNew */
NULL/*usbHidUrbNew*/,
/* pfnQueue */
usbHidQueue,
/* pfnUrbCancel */
usbHidUrbCancel,
/* pfnUrbReap */
usbHidUrbReap,
/* u32TheEnd */
PDM_USBREG_VERSION
};