Merge pull request #12719 from stblr/device-change-again

HIDv4: Fix racy device change behavior
This commit is contained in:
Tilka 2024-04-20 12:26:40 +01:00 committed by GitHub
commit b98176075e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 5 deletions

View File

@ -94,11 +94,12 @@ std::optional<IPCReply> USB_HIDv4::GetDeviceChange(const IOCtlRequest& request)
return IPCReply(IPC_EINVAL);
m_devicechange_hook_request = std::make_unique<IOCtlRequest>(GetSystem(), request.address);
// On the first call, the reply is sent immediately (instead of on device insertion/removal)
if (m_devicechange_first_call)
// If there are pending changes, the reply is sent immediately (instead of on device
// insertion/removal).
if (m_has_pending_changes)
{
TriggerDeviceChangeReply();
m_devicechange_first_call = false;
m_has_pending_changes = false;
}
return std::nullopt;
}
@ -138,7 +139,7 @@ s32 USB_HIDv4::SubmitTransfer(USB::Device& device, const IOCtlRequest& request)
void USB_HIDv4::DoState(PointerWrap& p)
{
p.Do(m_devicechange_first_call);
p.Do(m_has_pending_changes);
u32 hook_address = m_devicechange_hook_request ? m_devicechange_hook_request->address : 0;
p.Do(hook_address);
if (hook_address != 0)
@ -199,7 +200,10 @@ bool USB_HIDv4::ShouldAddDevice(const USB::Device& device) const
void USB_HIDv4::TriggerDeviceChangeReply()
{
if (!m_devicechange_hook_request)
{
m_has_pending_changes = true;
return;
}
auto& system = GetSystem();
auto& memory = system.GetMemory();

View File

@ -44,7 +44,7 @@ private:
static constexpr u32 VERSION = 0x40001;
static constexpr u8 HID_CLASS = 0x03;
bool m_devicechange_first_call = true;
bool m_has_pending_changes = true;
std::mutex m_devicechange_hook_address_mutex;
std::unique_ptr<IOCtlRequest> m_devicechange_hook_request;