VirtualBox

Changeset 46730 in vbox for trunk/src/VBox/Devices


Ignore:
Timestamp:
Jun 21, 2013 10:53:46 PM (11 years ago)
Author:
vboxsync
Message:

Devices/Input/UsbMouse: re-factor code to make it easier to put it inside a unit test.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Input/UsbMouse.cpp

    r46516 r46730  
    120120typedef struct USBHID
    121121{
    122     /** Pointer back to the PDM USB Device instance structure. */
    123     PPDMUSBINS          pUsbIns;
     122    /** USB device instance number. */
     123    uint32_t            iInstance;
    124124    /** Critical section protecting the device state. */
    125125    RTCRITSECT          CritSect;
     
    634634static int usbHidCompleteStall(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb, const char *pszWhy)
    635635{
    636     Log(("usbHidCompleteStall/#%u: pUrb=%p:%s: %s\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, pszWhy));
     636    Log(("usbHidCompleteStall/#%u: pUrb=%p:%s: %s\n", pThis->iInstance, pUrb, pUrb->pszDesc, pszWhy));
    637637
    638638    pUrb->enmStatus = VUSBSTATUS_STALL;
     
    657657static int usbHidCompleteOk(PUSBHID pThis, PVUSBURB pUrb, size_t cbData)
    658658{
    659     Log(("usbHidCompleteOk/#%u: pUrb=%p:%s cbData=%#zx\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, cbData));
     659    Log(("usbHidCompleteOk/#%u: pUrb=%p:%s cbData=%#zx\n", pThis->iInstance, pUrb, pUrb->pszDesc, cbData));
    660660
    661661    pUrb->enmStatus = VUSBSTATUS_OK;
     
    859859}
    860860
    861 /**
    862  * @copydoc PDMUSBREG::pfnUrbReap
    863  */
    864 static DECLCALLBACK(PVUSBURB) usbHidUrbReap(PPDMUSBINS pUsbIns, RTMSINTERVAL cMillies)
    865 {
    866     PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
    867     LogFlow(("usbHidUrbReap/#%u: cMillies=%u\n", pUsbIns->iInstance, cMillies));
    868 
     861
     862static PVUSBURB usbHidUrbReapCore(PUSBHID pThis, RTMSINTERVAL cMillies)
     863{
    869864    RTCritSectEnter(&pThis->CritSect);
    870865
     
    887882
    888883    if (pUrb)
    889         Log(("usbHidUrbReap/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
     884        Log(("usbHidUrbReap/#%u: pUrb=%p:%s\n", pThis->iInstance, pUrb, pUrb->pszDesc));
    890885    return pUrb;
    891886}
     
    893888
    894889/**
    895  * @copydoc PDMUSBREG::pfnUrbCancel
    896  */
    897 static DECLCALLBACK(int) usbHidUrbCancel(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
     890 * @copydoc PDMUSBREG::pfnUrbReap
     891 */
     892static DECLCALLBACK(PVUSBURB) usbHidUrbReap(PPDMUSBINS pUsbIns,
     893                                            RTMSINTERVAL cMillies)
    898894{
    899895    PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
    900     LogFlow(("usbHidUrbCancel/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
     896    LogFlow(("usbHidUrbReap/#%u: cMillies=%u\n", pThis->iInstance, cMillies));
     897    return usbHidUrbReapCore(pThis, cMillies);
     898}
     899
     900
     901static int usbHidUrbCancelCore(PUSBHID pThis, PVUSBURB pUrb)
     902{
    901903    RTCritSectEnter(&pThis->CritSect);
    902904
     
    911913}
    912914
     915
     916/**
     917 * @copydoc PDMUSBREG::pfnUrbCancel
     918 */
     919static DECLCALLBACK(int) usbHidUrbCancel(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
     920{
     921    PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
     922    LogFlow(("usbHidUrbCancel/#%u: pUrb=%p:%s\n", pThis->iInstance, pUrb,
     923             pUrb->pszDesc));
     924    return usbHidUrbCancelCore(pThis, pUrb);
     925}
    913926
    914927/**
     
    11491162
    11501163
    1151 /**
    1152  * @copydoc PDMUSBREG::pfnUrbQueue
    1153  */
    1154 static DECLCALLBACK(int) usbHidQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
    1155 {
    1156     PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
    1157     LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
     1164static int usbHidQueueCore(PUSBHID pThis, PVUSBURB pUrb)
     1165{
    11581166    RTCritSectEnter(&pThis->CritSect);
    11591167
     
    11861194
    11871195/**
    1188  * @copydoc PDMUSBREG::pfnUsbClearHaltedEndpoint
    1189  */
    1190 static DECLCALLBACK(int) usbHidUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns, unsigned uEndpoint)
     1196 * @copydoc PDMUSBREG::pfnUrbQueue
     1197 */
     1198static DECLCALLBACK(int) usbHidQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
    11911199{
    11921200    PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
    1193     LogFlow(("usbHidUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n", pUsbIns->iInstance, uEndpoint));
    1194 
     1201    LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance,
     1202             pUrb, pUrb->pszDesc, pUrb->EndPt));
     1203    return usbHidQueueCore(pThis, pUrb);
     1204}
     1205
     1206
     1207static int usbHidUsbClearHaltedEndpointCore(PUSBHID pThis, unsigned uEndpoint)
     1208{
    11951209    if ((uEndpoint & ~0x80) < RT_ELEMENTS(pThis->aEps))
    11961210    {
     
    12051219
    12061220/**
     1221 * @copydoc PDMUSBREG::pfnUsbClearHaltedEndpoint
     1222 */
     1223static DECLCALLBACK(int) usbHidUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns,
     1224                                                      unsigned uEndpoint)
     1225{
     1226    PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
     1227    LogFlow(("usbHidUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n",
     1228             pUsbIns->iInstance, uEndpoint));
     1229    return usbHidUsbClearHaltedEndpointCore(pThis, uEndpoint);
     1230}
     1231
     1232
     1233/**
    12071234 * @copydoc PDMUSBREG::pfnUsbSetInterface
    12081235 */
    1209 static DECLCALLBACK(int) usbHidUsbSetInterface(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
    1210 {
    1211     LogFlow(("usbHidUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n", pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
     1236static DECLCALLBACK(int) usbHidUsbSetInterface(PPDMUSBINS pUsbIns,
     1237                                               uint8_t bInterfaceNumber,
     1238                                               uint8_t bAlternateSetting)
     1239{
     1240    LogFlow(("usbHidUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n",
     1241             pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
    12121242    Assert(bAlternateSetting == 0);
    12131243    return VINF_SUCCESS;
     
    12151245
    12161246
    1217 /**
    1218  * @copydoc PDMUSBREG::pfnUsbSetConfiguration
    1219  */
    1220 static DECLCALLBACK(int) usbHidUsbSetConfiguration(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
    1221                                                    const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc)
    1222 {
    1223     PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
    1224     LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
     1247static int usbHidUsbSetConfigurationCore(PUSBHID pThis,
     1248                                         uint8_t bConfigurationValue,
     1249                                         const void *pvOldCfgDesc,
     1250                                         const void *pvOldIfState,
     1251                                         const void *pvNewCfgDesc)
     1252{
    12251253    Assert(bConfigurationValue == 1);
    12261254    RTCritSectEnter(&pThis->CritSect);
     
    12301258     */
    12311259    if (pThis->bConfigurationValue == bConfigurationValue)
    1232         usbHidResetWorker(pThis, NULL, true /*fSetConfig*/); /** @todo figure out the exact difference */
     1260        usbHidResetWorker(pThis, NULL, true /*fSetConfig*/);
     1261        /** @todo figure out the exact difference */
    12331262    pThis->bConfigurationValue = bConfigurationValue;
    12341263
     
    12451274
    12461275/**
     1276 * @copydoc PDMUSBREG::pfnUsbSetConfiguration
     1277 */
     1278static DECLCALLBACK(int) usbHidUsbSetConfiguration(PPDMUSBINS pUsbIns,
     1279                                                   uint8_t bConfigurationValue,
     1280                                                   const void *pvOldCfgDesc,
     1281                                                   const void *pvOldIfState,
     1282                                                   const void *pvNewCfgDesc)
     1283{
     1284    PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
     1285    LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n",
     1286             pUsbIns->iInstance, bConfigurationValue));
     1287    return usbHidUsbSetConfigurationCore(pThis, bConfigurationValue,
     1288                                         pvOldCfgDesc, pvOldIfState,
     1289                                         pvNewCfgDesc);
     1290}
     1291
     1292
     1293/**
    12471294 * @copydoc PDMUSBREG::pfnUsbGetDescriptorCache
    12481295 */
    1249 static DECLCALLBACK(PCPDMUSBDESCCACHE) usbHidUsbGetDescriptorCache(PPDMUSBINS pUsbIns)
    1250 {
    1251     PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
    1252     LogFlow(("usbHidUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
     1296static PCPDMUSBDESCCACHE usbHidUsbGetDescriptorCacheCore(PUSBHID pThis)
     1297{
    12531298    if (pThis->isAbsolute) {
    12541299        return &g_UsbHidTDescCache;
     
    12601305
    12611306/**
     1307 * @copydoc PDMUSBREG::pfnUsbGetDescriptorCache
     1308 */
     1309static DECLCALLBACK(PCPDMUSBDESCCACHE) usbHidUsbGetDescriptorCache(PPDMUSBINS
     1310                                                                        pUsbIns)
     1311{
     1312    PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
     1313    LogFlow(("usbHidUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
     1314    return usbHidUsbGetDescriptorCacheCore(pThis);
     1315}
     1316
     1317
     1318static DECLCALLBACK(int) usbHidUsbResetCore(PUSBHID pThis)
     1319{
     1320    RTCritSectEnter(&pThis->CritSect);
     1321
     1322    int rc = usbHidResetWorker(pThis, NULL, false /*fSetConfig*/);
     1323
     1324    RTCritSectLeave(&pThis->CritSect);
     1325    return rc;
     1326}
     1327
     1328
     1329/**
    12621330 * @copydoc PDMUSBREG::pfnUsbReset
    12631331 */
     
    12661334    PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
    12671335    LogFlow(("usbHidUsbReset/#%u:\n", pUsbIns->iInstance));
    1268     RTCritSectEnter(&pThis->CritSect);
    1269 
    1270     int rc = usbHidResetWorker(pThis, NULL, false /*fSetConfig*/);
    1271 
    1272     RTCritSectLeave(&pThis->CritSect);
    1273     return rc;
    1274 }
    1275 
    1276 
    1277 /**
    1278  * @copydoc PDMUSBREG::pfnDestruct
    1279  */
    1280 static void usbHidDestruct(PPDMUSBINS pUsbIns)
    1281 {
    1282     PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
    1283     LogFlow(("usbHidDestruct/#%u:\n", pUsbIns->iInstance));
    1284 
     1336    return usbHidUsbResetCore(pThis);
     1337}
     1338
     1339
     1340static void usbHidDestructCore(PUSBHID pThis)
     1341{
    12851342    if (RTCritSectIsInitialized(&pThis->CritSect))
    12861343    {
     
    12991356
    13001357/**
    1301  * @copydoc PDMUSBREG::pfnConstruct
    1302  */
    1303 static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
     1358 * @copydoc PDMUSBREG::pfnDestruct
     1359 */
     1360static DECLCALLBACK(void) usbHidDestruct(PPDMUSBINS pUsbIns)
    13041361{
    13051362    PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
    1306     Log(("usbHidConstruct/#%u:\n", iInstance));
    1307 
     1363    LogFlow(("usbHidDestruct/#%u:\n", pUsbIns->iInstance));
     1364    usbHidDestructCore(pThis);
     1365}
     1366
     1367
     1368static int usbHidConstructCore(PUSBHID pThis, int iInstance, bool isAbsolute,
     1369                               uint8_t u8CoordShift)
     1370{
    13081371    /*
    13091372     * Perform the basic structure initialization first so the destructor
    13101373     * will not misbehave.
    13111374     */
    1312     pThis->pUsbIns                                  = pUsbIns;
     1375    pThis->iInstance                                = iInstance;
    13131376    pThis->hEvtDoneQueue                            = NIL_RTSEMEVENT;
    13141377    usbHidQueueInit(&pThis->ToHostQueue);
     
    13211384    AssertRCReturn(rc, rc);
    13221385
     1386    pThis->isAbsolute = isAbsolute;
     1387    pThis->u8CoordShift = u8CoordShift;
     1388
     1389    pThis->Lun0.IBase.pfnQueryInterface = usbHidMouseQueryInterface;
     1390    pThis->Lun0.IPort.pfnPutEvent       = usbHidMousePutEvent;
     1391    pThis->Lun0.IPort.pfnPutEventAbs    = usbHidMousePutEventAbs;
     1392
     1393    return VINF_SUCCESS;
     1394}
     1395
     1396
     1397static void usbHidConstructFinish(PUSBHID pThis, PPDMIMOUSECONNECTOR pDrv)
     1398{
     1399    pThis->Lun0.pDrv = pDrv;
     1400}
     1401
     1402
     1403/**
     1404 * @copydoc PDMUSBREG::pfnConstruct
     1405 */
     1406static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance,
     1407                                         PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
     1408{
     1409    PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
     1410    bool isAbsolute;
     1411    uint8_t u8CoordShift;
     1412    PPDMIMOUSECONNECTOR pDrv;
     1413    Log(("usbHidConstruct/#%u:\n", iInstance));
     1414
    13231415    /*
    13241416     * Validate and read the configuration.
    13251417     */
    1326     rc = CFGMR3ValidateConfig(pCfg, "/", "Absolute|CoordShift", "Config", "UsbHid", iInstance);
     1418    int rc = CFGMR3ValidateConfig(pCfg, "/", "Absolute|CoordShift", "Config",
     1419                                  "UsbHid", iInstance);
    13271420    if (RT_FAILURE(rc))
    13281421        return rc;
    1329     rc = CFGMR3QueryBoolDef(pCfg, "Absolute", &pThis->isAbsolute, false);
     1422    rc = CFGMR3QueryBoolDef(pCfg, "Absolute", &isAbsolute, false);
    13301423    if (RT_FAILURE(rc))
    1331         return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to query settings"));
    1332 
    1333     pThis->Lun0.IBase.pfnQueryInterface = usbHidMouseQueryInterface;
    1334     pThis->Lun0.IPort.pfnPutEvent       = usbHidMousePutEvent;
    1335     pThis->Lun0.IPort.pfnPutEventAbs    = usbHidMousePutEventAbs;
     1424        return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS,
     1425                                   N_("HID failed to query settings"));
     1426
     1427    rc = CFGMR3QueryU8Def(pCfg, "CoordShift", &u8CoordShift, 1);
     1428    if (RT_FAILURE(rc))
     1429        return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS,
     1430                                   N_("HID failed to query shift factor"));
     1431
     1432    rc = usbHidConstructCore(pThis, iInstance, isAbsolute, u8CoordShift);
    13361433
    13371434    /*
    13381435     * Attach the mouse driver.
    13391436     */
    1340     rc = PDMUsbHlpDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "Mouse Port");
     1437    rc = PDMUsbHlpDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase,
     1438                               &pThis->Lun0.pDrvBase, "Mouse Port");
    13411439    if (RT_FAILURE(rc))
    1342         return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to attach mouse driver"));
    1343 
    1344     pThis->Lun0.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pDrvBase, PDMIMOUSECONNECTOR);
    1345     if (!pThis->Lun0.pDrv)
    1346         return PDMUsbHlpVMSetError(pUsbIns, VERR_PDM_MISSING_INTERFACE, RT_SRC_POS, N_("HID failed to query mouse interface"));
    1347 
    1348     rc = CFGMR3QueryU8Def(pCfg, "CoordShift", &pThis->u8CoordShift, 1);
    1349     if (RT_FAILURE(rc))
    1350         return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to query shift factor"));
     1440        return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS,
     1441                                   N_("HID failed to attach mouse driver"));
     1442
     1443    pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pDrvBase, PDMIMOUSECONNECTOR);
     1444    if (!pDrv)
     1445        return PDMUsbHlpVMSetError(pUsbIns, VERR_PDM_MISSING_INTERFACE,
     1446                                   RT_SRC_POS,
     1447                                   N_("HID failed to query mouse interface"));
     1448    usbHidConstructFinish(pThis, pDrv);
    13511449
    13521450    return VINF_SUCCESS;
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette