Changeset 48062 in vbox
- Timestamp:
- Aug 26, 2013 2:12:53 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/HostDrivers/VBoxUSB/win/mon/VBoxUsbMon.cpp
r38616 r48062 62 62 } VBOXUSBHUB_PNPHOOK_COMPLETION, *PVBOXUSBHUB_PNPHOOK_COMPLETION; 63 63 64 /* 65 * Comment out VBOX_USB3PORT definition to disable hooking to multiple drivers (#6509) 66 */ 67 #define VBOX_USB3PORT 68 69 #ifdef VBOX_USB3PORT 70 #define VBOXUSBMON_MAXDRIVERS 3 71 typedef struct VBOXUSB_PNPDRIVER 72 { 73 PDRIVER_OBJECT DriverObject; 74 VBOXUSBHUB_PNPHOOK UsbHubPnPHook; 75 PDRIVER_DISPATCH pfnHookStub; 76 } VBOXUSB_PNPDRIVER, *PVBOXUSB_PNPDRIVER; 77 #endif /* !VBOX_USB3PORT */ 78 64 79 typedef struct VBOXUSBMONGLOBALS 65 80 { 66 81 PDEVICE_OBJECT pDevObj; 82 #ifdef VBOX_USB3PORT 83 VBOXUSB_PNPDRIVER pDrivers[VBOXUSBMON_MAXDRIVERS]; 84 #else /* !VBOX_USB3PORT */ 67 85 VBOXUSBHUB_PNPHOOK UsbHubPnPHook; 86 #endif /* !VBOX_USB3PORT */ 68 87 KEVENT OpenSynchEvent; 69 88 IO_REMOVE_LOCK RmLock; … … 510 529 { 511 530 NTSTATUS Status = STATUS_UNSUCCESSFUL; 531 #ifndef VBOX_USB3PORT 512 532 UNICODE_STRING szStandardHubName; 513 533 PDRIVER_OBJECT pDrvObj = NULL; … … 587 607 } 588 608 } 609 #else /* VBOX_USB3PORT */ 610 PWSTR szwHubList; 611 Status = IoGetDeviceInterfaces(&GUID_DEVINTERFACE_USB_HUB, NULL, 0, &szwHubList); 612 if (Status != STATUS_SUCCESS) 613 { 614 LOG(("IoGetDeviceInterfaces failed with %d\n", Status)); 615 return; 616 } 617 if (szwHubList) 618 { 619 UNICODE_STRING UnicodeName; 620 PDEVICE_OBJECT pHubDevObj; 621 PFILE_OBJECT pHubFileObj; 622 PWSTR szwHubName = szwHubList; 623 while (*szwHubName != UNICODE_NULL) 624 { 625 RtlInitUnicodeString(&UnicodeName, szwHubName); 626 Status = IoGetDeviceObjectPointer(&UnicodeName, FILE_READ_DATA, &pHubFileObj, &pHubDevObj); 627 if (Status == STATUS_SUCCESS) 628 { 629 /** @todo Replace %S with something else as it does not work for PWSTR. */ 630 LOG(("IoGetDeviceObjectPointer for %S returned %p %p", szwHubName, pHubDevObj, pHubFileObj)); 631 if (!pfnWalker(pHubFileObj, pHubDevObj, pHubDevObj, pvWalker)) 632 { 633 LOG(("the walker said to stop")); 634 ObDereferenceObject(pHubFileObj); 635 break; 636 } 637 638 LOG(("going forward..")); 639 ObDereferenceObject(pHubFileObj); 640 } 641 szwHubName += wcslen(szwHubName) + 1; 642 } 643 ExFreePool(szwHubList); 644 } 645 #endif /* VBOX_USB3PORT */ 589 646 } 590 647 … … 997 1054 NTSTATUS tmpStatus = pIrp->IoStatus.Status; 998 1055 #endif 999 NTSTATUS Status = VBoxUsbHookRequestComplete(&g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook, pDevObj, pIrp, pRequest); 1056 #ifdef VBOX_USB3PORT 1057 PVBOXUSBHOOK_ENTRY pHook = pRequest->pHook; 1058 #else /* !VBOX_USB3PORT */ 1059 PVBOXUSBHOOK_ENTRY pHook = &g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook; 1060 #endif /* !VBOX_USB3PORT */ 1061 NTSTATUS Status = VBoxUsbHookRequestComplete(pHook, pDevObj, pIrp, pRequest); 1000 1062 VBoxUsbMonMemFree(pRequest); 1001 1063 #ifdef DEBUG_misha … … 1005 1067 } 1006 1068 #endif 1007 VBoxUsbHookRelease( &g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook);1069 VBoxUsbHookRelease(pHook); 1008 1070 return Status; 1009 1071 } … … 1015 1077 * @param pIrp Request packet. 1016 1078 */ 1079 #ifdef VBOX_USB3PORT 1080 static NTSTATUS vboxUsbMonPnPHook(IN PVBOXUSBHOOK_ENTRY pHook, IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) 1081 #else /* !VBOX_USB3PORT */ 1017 1082 NTSTATUS _stdcall VBoxUsbMonPnPHook(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) 1018 { 1083 #endif /* !VBOX_USB3PORT */ 1084 { 1085 #ifndef VBOX_USB3PORT 1086 PVBOXUSBHOOK_ENTRY pHook = &g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook; 1087 #endif /* !VBOX_USB3PORT */ 1019 1088 LOG(("==>PnP: Mn(%s), PDO(0x%p), IRP(0x%p), Status(0x%x)", vboxUsbDbgStrPnPMn(IoGetCurrentIrpStackLocation(pIrp)->MinorFunction), pDevObj, pIrp, pIrp->IoStatus.Status)); 1020 1089 1021 if(!VBoxUsbHookRetain( &g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook))1090 if(!VBoxUsbHookRetain(pHook)) 1022 1091 { 1023 1092 WARN(("VBoxUsbHookRetain failed")); 1024 return VBoxUsbHookRequestPassDownHookSkip( &g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook, pDevObj, pIrp);1093 return VBoxUsbHookRequestPassDownHookSkip(pHook, pDevObj, pIrp); 1025 1094 } 1026 1095 … … 1029 1098 { 1030 1099 WARN(("VBoxUsbMonMemAlloc failed")); 1031 VBoxUsbHookRelease( &g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook);1100 VBoxUsbHookRelease(pHook); 1032 1101 pIrp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES; 1033 1102 pIrp->IoStatus.Information = 0; … … 1036 1105 } 1037 1106 1038 NTSTATUS Status = VBoxUsbHookRequestPassDownHookCompletion( &g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook, pDevObj, pIrp, VBoxUsbPnPCompletion, &pCompletion->Rq);1107 NTSTATUS Status = VBoxUsbHookRequestPassDownHookCompletion(pHook, pDevObj, pIrp, VBoxUsbPnPCompletion, &pCompletion->Rq); 1039 1108 #ifdef VBOX_USB_WITH_VERBOSE_LOGGING 1040 1109 if (Status != STATUS_PENDING) 1041 1110 { 1042 1111 LOG(("Request completed, Status(0x%x)", Status)); 1043 VBoxUsbHookVerifyCompletion( &g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook, &pCompletion->Rq, pIrp);1112 VBoxUsbHookVerifyCompletion(pHook, &pCompletion->Rq, pIrp); 1044 1113 } 1045 1114 else … … 1051 1120 } 1052 1121 1122 #ifdef VBOX_USB3PORT 1123 /** 1124 * Device PnP hook stubs. 1125 * 1126 * @param pDevObj Device object. 1127 * @param pIrp Request packet. 1128 */ 1129 #define VBOX_PNPHOOKSTUB(n) NTSTATUS _stdcall VBoxUsbMonPnPHook##n(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) \ 1130 { \ 1131 return vboxUsbMonPnPHook(&g_VBoxUsbMonGlobals.pDrivers[n].UsbHubPnPHook.Hook, pDevObj, pIrp); \ 1132 } 1133 1134 #define VBOX_PNPHOOKSTUB_INIT(n) g_VBoxUsbMonGlobals.pDrivers[n].pfnHookStub = VBoxUsbMonPnPHook##n 1135 1136 VBOX_PNPHOOKSTUB(0) 1137 VBOX_PNPHOOKSTUB(1) 1138 VBOX_PNPHOOKSTUB(2) 1139 AssertCompile(VBOXUSBMON_MAXDRIVERS == 3); 1140 1141 typedef struct VBOXUSBMONHOOKDRIVERWALKER 1142 { 1143 PDRIVER_OBJECT pDrvObj; 1144 } VBOXUSBMONHOOKDRIVERWALKER, *PVBOXUSBMONHOOKDRIVERWALKER; 1145 1146 static DECLCALLBACK(BOOLEAN) vboxUsbMonHookDrvObjWalker(PFILE_OBJECT pFile, PDEVICE_OBJECT pTopDo, PDEVICE_OBJECT pHubDo, PVOID pvContext) 1147 { 1148 PDRIVER_OBJECT pDrvObj = pHubDo->DriverObject; 1149 1150 /* First we try to figure out if we are already hooked to this driver. */ 1151 for (int i = 0; i < VBOXUSBMON_MAXDRIVERS; i++) 1152 if (pDrvObj == g_VBoxUsbMonGlobals.pDrivers[i].DriverObject) 1153 { 1154 LOG(("Found %p at pDrivers[%d]\n", pDrvObj, i)); 1155 /* We've already hooked to this one -- nothing to do. */ 1156 return TRUE; 1157 } 1158 /* We are not hooked yet, find an empty slot. */ 1159 for (int i = 0; i < VBOXUSBMON_MAXDRIVERS; i++) 1160 { 1161 if (!g_VBoxUsbMonGlobals.pDrivers[i].DriverObject) 1162 { 1163 /* Found an emtpy slot, use it. */ 1164 g_VBoxUsbMonGlobals.pDrivers[i].DriverObject = pDrvObj; 1165 ObReferenceObject(pDrvObj); 1166 LOG(("pDrivers[%d] = %p, installing the hook...\n", i, pDrvObj)); 1167 VBoxUsbHookInit(&g_VBoxUsbMonGlobals.pDrivers[i].UsbHubPnPHook.Hook, 1168 pDrvObj, 1169 IRP_MJ_PNP, 1170 g_VBoxUsbMonGlobals.pDrivers[i].pfnHookStub); 1171 VBoxUsbHookInstall(&g_VBoxUsbMonGlobals.pDrivers[i].UsbHubPnPHook.Hook); 1172 return TRUE; /* Must continue to find all drivers. */ 1173 } 1174 if (pDrvObj == g_VBoxUsbMonGlobals.pDrivers[i].DriverObject) 1175 { 1176 LOG(("Found %p at pDrivers[%d]\n", pDrvObj, i)); 1177 /* We've already hooked to this one -- nothing to do. */ 1178 return TRUE; 1179 } 1180 } 1181 /* No empty slots! No reason to continue. */ 1182 LOG(("No empty slots!\n")); 1183 return FALSE; 1184 } 1185 1186 /** 1187 * Finds all USB drivers in the system and installs hooks if haven't done already. 1188 */ 1189 static NTSTATUS vboxUsbMonInstallAllHooks() 1190 { 1191 vboxUsbMonHubDevWalk(vboxUsbMonHookDrvObjWalker, NULL, VBOXUSBMONHUBWALK_F_ALL); 1192 return STATUS_SUCCESS; 1193 } 1194 #endif /* VBOX_USB3PORT */ 1053 1195 1054 1196 static NTSTATUS vboxUsbMonHookCheckInit() … … 1060 1202 return STATUS_SUCCESS; 1061 1203 } 1204 #ifdef VBOX_USB3PORT 1205 return vboxUsbMonInstallAllHooks(); 1206 #else /* !VBOX_USB3PORT */ 1062 1207 PDRIVER_OBJECT pDrvObj = vboxUsbMonHookFindHubDrvObj(); 1063 1208 if (pDrvObj) … … 1070 1215 WARN(("hub drv obj not found, fail")); 1071 1216 return STATUS_UNSUCCESSFUL; 1217 #endif /* !VBOX_USB3PORT */ 1072 1218 } 1073 1219 1074 1220 static NTSTATUS vboxUsbMonHookInstall() 1075 1221 { 1222 #ifdef VBOX_USB3PORT 1223 /* Nothing to do here as we have already installed all hooks in vboxUsbMonHookCheckInit(). */ 1224 return STATUS_SUCCESS; 1225 #else /* !VBOX_USB3PORT */ 1076 1226 #ifdef VBOXUSBMON_DBG_NO_PNPHOOK 1077 1227 return STATUS_SUCCESS; … … 1084 1234 return VBoxUsbHookInstall(&g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook); 1085 1235 #endif 1236 #endif /* !VBOX_USB3PORT */ 1086 1237 } 1087 1238 … … 1091 1242 return STATUS_SUCCESS; 1092 1243 #else 1244 #ifdef VBOX_USB3PORT 1245 NTSTATUS Status = STATUS_SUCCESS; 1246 for (int i = 0; i < VBOXUSBMON_MAXDRIVERS; i++) 1247 { 1248 if (g_VBoxUsbMonGlobals.pDrivers[i].DriverObject) 1249 { 1250 Assert(g_VBoxUsbMonGlobals.pDrivers[i].DriverObject == g_VBoxUsbMonGlobals.pDrivers[i].UsbHubPnPHook.Hook.pDrvObj); 1251 LOG(("Unhooking from %p...\n", g_VBoxUsbMonGlobals.pDrivers[i].DriverObject)); 1252 Status = VBoxUsbHookUninstall(&g_VBoxUsbMonGlobals.pDrivers[i].UsbHubPnPHook.Hook); 1253 if (!NT_SUCCESS(Status)) 1254 { 1255 /* 1256 * We failed to uninstall the hook, so we keep the reference to the driver 1257 * in order to prevent another driver re-using this slot because we are 1258 * going to mark this hook as fUninitFailed. 1259 */ 1260 //AssertMsgFailed(("usbhub pnp unhook failed, setting the fUninitFailed flag, the current value of fUninitFailed (%d)", g_VBoxUsbMonGlobals.UsbHubPnPHook.fUninitFailed)); 1261 LOG(("usbhub pnp unhook failed, setting the fUninitFailed flag, the current value of fUninitFailed (%d)", g_VBoxUsbMonGlobals.pDrivers[i].UsbHubPnPHook.fUninitFailed)); 1262 g_VBoxUsbMonGlobals.pDrivers[i].UsbHubPnPHook.fUninitFailed = true; 1263 } 1264 else 1265 { 1266 /* The hook was removed successfully, now we can forget about this driver. */ 1267 ObDereferenceObject(g_VBoxUsbMonGlobals.pDrivers[i].DriverObject); 1268 g_VBoxUsbMonGlobals.pDrivers[i].DriverObject = NULL; 1269 } 1270 } 1271 } 1272 #else /* !VBOX_USB3PORT */ 1093 1273 NTSTATUS Status = VBoxUsbHookUninstall(&g_VBoxUsbMonGlobals.UsbHubPnPHook.Hook); 1094 1274 if (!NT_SUCCESS(Status)) … … 1097 1277 g_VBoxUsbMonGlobals.UsbHubPnPHook.fUninitFailed = true; 1098 1278 } 1279 #endif /* !VBOX_USB3PORT */ 1099 1280 return Status; 1100 1281 #endif … … 1675 1856 1676 1857 memset (&g_VBoxUsbMonGlobals, 0, sizeof (g_VBoxUsbMonGlobals)); 1858 #ifdef VBOX_USB3PORT 1859 VBOX_PNPHOOKSTUB_INIT(0); 1860 VBOX_PNPHOOKSTUB_INIT(1); 1861 VBOX_PNPHOOKSTUB_INIT(2); 1862 AssertCompile(VBOXUSBMON_MAXDRIVERS == 3); 1863 #endif /* VBOX_USB3PORT */ 1677 1864 KeInitializeEvent(&g_VBoxUsbMonGlobals.OpenSynchEvent, SynchronizationEvent, TRUE /* signaled */); 1678 1865 IoInitializeRemoveLock(&g_VBoxUsbMonGlobals.RmLock, VBOXUSBMON_MEMTAG, 1, 100);
Note:
See TracChangeset
for help on using the changeset viewer.