void CUsbDkRedirectorStrategy::OnClose() { USB_DK_DEVICE_ID ID; UsbDkFillIDStruct(&ID, *m_DeviceID->begin(), *m_InstanceID->begin()); auto status = m_ControlDevice->RemoveRedirect(ID); if (!NT_SUCCESS(status)) { TraceEvents(TRACE_LEVEL_ERROR, TRACE_REDIRECTOR, "%!FUNC! RemoveRedirect failed: %!STATUS!", status); } }
bool CUsbDkFilterDevice::CStrategist::SelectStrategy(PDEVICE_OBJECT DevObj) { PAGED_CODE(); // 1. Get device ID CObjHolder<CRegText> DevID; if (!UsbDkGetWdmDeviceIdentity(DevObj, &DevID, nullptr)) { TraceEvents(TRACE_LEVEL_ERROR, TRACE_FILTERDEVICE, "%!FUNC! Cannot query device ID"); return false; } DevID->Dump(); // 2. Root hubs -> Hub strategy if ((DevID->Match(L"USB\\ROOT_HUB") || DevID->Match(L"USB\\ROOT_HUB20") || DevID->Match(L"USB\\ROOT_HUB30") || DevID->Match(L"NUSB3\\ROOT_HUB30") || DevID->Match(L"IUSB3\\ROOT_HUB30"))) { TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_FILTERDEVICE, "%!FUNC! Assigning HUB strategy"); m_Strategy->Delete(); m_Strategy = &m_HubStrategy; return true; } // 3. Not a USB device -> do not filter if (!DevID->MatchPrefix(L"USB\\")) { TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_FILTERDEVICE, "%!FUNC! Not a usb device, no strategy assigned"); return false; } // 4. Get instance ID CObjHolder<CRegText> InstanceID; if (!UsbDkGetWdmDeviceIdentity(DevObj, nullptr, &InstanceID)) { TraceEvents(TRACE_LEVEL_ERROR, TRACE_FILTERDEVICE, "%!FUNC! Cannot query instance ID"); return false; } USB_DK_DEVICE_ID ID; UsbDkFillIDStruct(&ID, *DevID->begin(), *InstanceID->begin()); // 5. Get device descriptor USB_DEVICE_DESCRIPTOR DevDescr; if (!m_Strategy->GetControlDevice()->GetDeviceDescriptor(ID, DevDescr)) { TraceEvents(TRACE_LEVEL_ERROR, TRACE_FILTERDEVICE, "%!FUNC! Cannot query cached device descriptor"); return false; } // 6. Device class is HUB -> Hub strategy if (DevDescr.bDeviceClass == USB_DEVICE_CLASS_HUB) { TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_FILTERDEVICE, "%!FUNC! Device class is HUB, assigning hub strategy"); m_Strategy->Delete(); m_Strategy = &m_HubStrategy; return true; } // 7. Configuration doesn't tell to redirect or device already redirected -> no strategy if (!m_Strategy->GetControlDevice()->ShouldRedirect(ID)) { TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_FILTERDEVICE, "%!FUNC! Do not redirect or already redirected device, no strategy assigned"); return false; } // 8. Redirector strategy TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_FILTERDEVICE, "%!FUNC! Assigning redirected USB device strategy"); m_DevStrategy.SetDeviceID(DevID.detach()); m_DevStrategy.SetInstanceID(InstanceID.detach()); m_Strategy->Delete(); m_Strategy = &m_DevStrategy; return true; }