diff --git a/headers/addons/keyboard_host_listener.h b/headers/addons/keyboard_host_listener.h index 3f7df72aa..044a163fa 100644 --- a/headers/addons/keyboard_host_listener.h +++ b/headers/addons/keyboard_host_listener.h @@ -58,6 +58,9 @@ class KeyboardHostListener : public USBListener { KeyboardButtonMapping _keyboard_host_mapButtonR3; KeyboardButtonMapping _keyboard_host_mapButtonA1; KeyboardButtonMapping _keyboard_host_mapButtonA2; + + uint8_t _keyboard_dev_addr; + uint8_t _keyboard_instance; }; #endif // _KeyboardHost_H_ \ No newline at end of file diff --git a/headers/drivers/ps4/PS4AuthUSBListener.h b/headers/drivers/ps4/PS4AuthUSBListener.h index 028024810..8282423ed 100644 --- a/headers/drivers/ps4/PS4AuthUSBListener.h +++ b/headers/drivers/ps4/PS4AuthUSBListener.h @@ -30,6 +30,7 @@ class PS4AuthUSBListener : public USBListener { bool awaiting_cb; bool ps4_auth_host_ready; uint8_t * ps4_auth_buffer; + bool ps_mounted; }; #endif // _PS4AUTHUSBLISTENER_H_ diff --git a/src/addons/keyboard_host_listener.cpp b/src/addons/keyboard_host_listener.cpp index eda62f2d3..f4c8821a3 100644 --- a/src/addons/keyboard_host_listener.cpp +++ b/src/addons/keyboard_host_listener.cpp @@ -45,6 +45,8 @@ void KeyboardHostListener::setup() { _keyboard_host_mapButtonA2.setKey(keyboardMapping.keyButtonA2); _keyboard_host_enabled = false; + _keyboard_dev_addr = 0; + _keyboard_instance = 0; } void KeyboardHostListener::process() { @@ -62,15 +64,29 @@ void KeyboardHostListener::process() { } void KeyboardHostListener::mount(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) { - _keyboard_host_enabled = true; + // Interface protocol (hid_interface_protocol_enum_t) + uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance); + + // tuh_hid_report_received_cb() will be invoked when report is available + if (itf_protocol != HID_ITF_PROTOCOL_KEYBOARD) + return; + + _keyboard_dev_addr = dev_addr; + _keyboard_instance = instance; + _keyboard_host_enabled = true; } void KeyboardHostListener::unmount(uint8_t dev_addr) { - _keyboard_host_enabled = false; + if ( _keyboard_dev_addr == dev_addr ) { + _keyboard_host_enabled = false; + _keyboard_dev_addr = 0; + _keyboard_instance = 0; + } } void KeyboardHostListener::report_received(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len){ - if ( _keyboard_host_enabled == false ) + if ( _keyboard_host_enabled == false || + _keyboard_dev_addr != dev_addr || _keyboard_instance != instance ) return; // do nothing if we haven't mounted // Interface protocol (hid_interface_protocol_enum_t) diff --git a/src/drivers/ps4/PS4AuthUSBListener.cpp b/src/drivers/ps4/PS4AuthUSBListener.cpp index 57796cdee..63429915c 100644 --- a/src/drivers/ps4/PS4AuthUSBListener.cpp +++ b/src/drivers/ps4/PS4AuthUSBListener.cpp @@ -15,6 +15,9 @@ void PS4AuthUSBListener::setup() { passthrough_state = PS4State::no_nonce; ps4_auth_host_ready = false; ps4_auth_buffer = nullptr; + ps_dev_addr = 0xFF; + ps_instance = 0xFF; + ps_mounted = false; } void PS4AuthUSBListener::process(PS4State pState, uint8_t pNonceId, uint8_t * pNonceBuffer) { @@ -92,8 +95,27 @@ bool PS4AuthUSBListener::host_set_report(uint8_t report_id, void* report, uint16 } void PS4AuthUSBListener::mount(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) { + // Prevent Magic-X double mount + if ( ps_mounted == true ) + return; + + // Only a PS4 interface has vendor IDs F0, F1, F2, and F3 + tuh_hid_report_info_t report_info[4]; + uint8_t report_count = tuh_hid_parse_report_descriptor(report_info, 4, desc_report, desc_len); + bool ps4Dongle = false; + for(uint8_t i = 0; i < report_count; i++) { + if ( report_info[i].usage_page == 0xFFF0 && + (report_info[i].report_id == 0xF3) ) { + ps4Dongle = true; + break; + } + } + if (ps4Dongle == false ) + return; + ps_dev_addr = dev_addr; ps_instance = instance; + ps_mounted = true; // Reset as soon as its connected memset(report_buffer, 0, sizeof(report_buffer)); @@ -102,14 +124,23 @@ void PS4AuthUSBListener::mount(uint8_t dev_addr, uint8_t instance, uint8_t const } void PS4AuthUSBListener::unmount(uint8_t dev_addr) { + if ( ps_mounted == false || dev_addr != ps_dev_addr ) + return; + nonce_page = 0; // no nonce yet send_nonce_part = 0; // which part of the nonce are we getting from send? awaiting_cb = false; // did we receive the sign state yet passthrough_state = PS4State::no_nonce; ps4_auth_host_ready = false; + ps_dev_addr = 0xFF; + ps_instance = 0xFF; + ps_mounted = false; } void PS4AuthUSBListener::set_report_complete(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len) { + if ( ps_mounted == false || dev_addr != ps_dev_addr || instance != ps_instance ) + return; + switch(report_id) { case PS4AuthReport::PS4_SET_AUTH_PAYLOAD: if (nonce_page == 5) { @@ -124,6 +155,9 @@ void PS4AuthUSBListener::set_report_complete(uint8_t dev_addr, uint8_t instance, } void PS4AuthUSBListener::get_report_complete(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len) { + if ( ps_mounted == false || dev_addr != ps_dev_addr || instance != ps_instance ) + return; + switch(report_id) { case PS4AuthReport::PS4_DEFINITION: break; diff --git a/src/drivers/xbone/XBOneAuthUSBListener.cpp b/src/drivers/xbone/XBOneAuthUSBListener.cpp index d7c4a439e..5c1cc8104 100644 --- a/src/drivers/xbone/XBOneAuthUSBListener.cpp +++ b/src/drivers/xbone/XBOneAuthUSBListener.cpp @@ -29,6 +29,8 @@ static uint32_t lastReportQueue = 0; void XBOneAuthUSBListener::setup() { xboxOneAuthData = nullptr; + xbone_dev_addr = 0; + xbone_instance = 0; } void XBOneAuthUSBListener::setAuthData(XboxOneAuthData * authData ) { @@ -88,7 +90,7 @@ void XBOneAuthUSBListener::unmount(uint8_t dev_addr) { } void XBOneAuthUSBListener::report_received(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) { - if ( mounted == false || xboxOneAuthData == nullptr || dev_addr != xbone_dev_addr) { + if ( mounted == false || xboxOneAuthData == nullptr || dev_addr != xbone_dev_addr || instance != xbone_instance ) { return; }