mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-31 18:19:49 -06:00
Merge pull request #5170 from leoetlino/bt-fix
libusb fixes (mostly for IOS)
This commit is contained in:
@ -15,7 +15,6 @@
|
||||
|
||||
#include "Common/Assert.h"
|
||||
#include "Common/ChunkFile.h"
|
||||
#include "Common/LibusbContext.h"
|
||||
#include "Common/Logging/Log.h"
|
||||
#include "Common/MsgHandler.h"
|
||||
#include "Common/Network.h"
|
||||
@ -60,7 +59,11 @@ namespace Device
|
||||
BluetoothReal::BluetoothReal(u32 device_id, const std::string& device_name)
|
||||
: BluetoothBase(device_id, device_name)
|
||||
{
|
||||
m_libusb_context = LibusbContext::Get();
|
||||
const int ret = libusb_init(&m_libusb_context);
|
||||
if (ret < 0)
|
||||
{
|
||||
PanicAlertT("Couldn't initialise libusb for Bluetooth passthrough: %s", libusb_error_name(ret));
|
||||
}
|
||||
LoadLinkKeys();
|
||||
}
|
||||
|
||||
@ -77,6 +80,7 @@ BluetoothReal::~BluetoothReal()
|
||||
libusb_unref_device(m_device);
|
||||
}
|
||||
|
||||
libusb_exit(m_libusb_context);
|
||||
SaveLinkKeys();
|
||||
}
|
||||
|
||||
@ -86,15 +90,21 @@ ReturnCode BluetoothReal::Open(const OpenRequest& request)
|
||||
return IPC_EACCES;
|
||||
|
||||
libusb_device** list;
|
||||
const ssize_t cnt = libusb_get_device_list(m_libusb_context.get(), &list);
|
||||
_dbg_assert_msg_(IOS, cnt > 0, "Couldn't get device list");
|
||||
const ssize_t cnt = libusb_get_device_list(m_libusb_context, &list);
|
||||
if (cnt < 0)
|
||||
{
|
||||
ERROR_LOG(IOS_WIIMOTE, "Couldn't get device list: %s",
|
||||
libusb_error_name(static_cast<int>(cnt)));
|
||||
return IPC_ENOENT;
|
||||
}
|
||||
|
||||
for (ssize_t i = 0; i < cnt; ++i)
|
||||
{
|
||||
libusb_device* device = list[i];
|
||||
libusb_device_descriptor device_descriptor;
|
||||
libusb_config_descriptor* config_descriptor;
|
||||
libusb_get_device_descriptor(device, &device_descriptor);
|
||||
const int ret = libusb_get_active_config_descriptor(device, &config_descriptor);
|
||||
const int ret = libusb_get_config_descriptor(device, 0, &config_descriptor);
|
||||
if (ret != 0)
|
||||
{
|
||||
ERROR_LOG(IOS_WIIMOTE, "Failed to get config descriptor for device %04x:%04x: %s",
|
||||
@ -601,7 +611,7 @@ void BluetoothReal::TransferThread()
|
||||
Common::SetCurrentThreadName("BT USB Thread");
|
||||
while (m_thread_running.IsSet())
|
||||
{
|
||||
libusb_handle_events_completed(m_libusb_context.get(), nullptr);
|
||||
libusb_handle_events_completed(m_libusb_context, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,7 +76,7 @@ private:
|
||||
|
||||
libusb_device* m_device = nullptr;
|
||||
libusb_device_handle* m_handle = nullptr;
|
||||
std::shared_ptr<libusb_context> m_libusb_context;
|
||||
libusb_context* m_libusb_context = nullptr;
|
||||
|
||||
Common::Flag m_thread_running;
|
||||
std::thread m_thread;
|
||||
|
@ -13,7 +13,6 @@
|
||||
#include "Common/Assert.h"
|
||||
#include "Common/ChunkFile.h"
|
||||
#include "Common/CommonTypes.h"
|
||||
#include "Common/LibusbContext.h"
|
||||
#include "Common/Logging/Log.h"
|
||||
#include "Common/Thread.h"
|
||||
#include "Core/ConfigManager.h"
|
||||
@ -30,6 +29,18 @@ namespace Device
|
||||
{
|
||||
USBHost::USBHost(u32 device_id, const std::string& device_name) : Device(device_id, device_name)
|
||||
{
|
||||
#ifdef __LIBUSB__
|
||||
const int ret = libusb_init(&m_libusb_context);
|
||||
_dbg_assert_msg_(IOS_USB, ret == 0, "Failed to init libusb for USB passthrough.");
|
||||
#endif
|
||||
}
|
||||
|
||||
USBHost::~USBHost()
|
||||
{
|
||||
#ifdef __LIBUSB__
|
||||
if (m_libusb_context)
|
||||
libusb_exit(m_libusb_context);
|
||||
#endif
|
||||
}
|
||||
|
||||
ReturnCode USBHost::Open(const OpenRequest& request)
|
||||
@ -116,11 +127,10 @@ bool USBHost::AddNewDevices(std::set<u64>& new_devices, DeviceChangeHooks& hooks
|
||||
if (SConfig::GetInstance().m_usb_passthrough_devices.empty())
|
||||
return true;
|
||||
|
||||
auto libusb_context = LibusbContext::Get();
|
||||
if (libusb_context)
|
||||
if (m_libusb_context)
|
||||
{
|
||||
libusb_device** list;
|
||||
const ssize_t count = libusb_get_device_list(libusb_context.get(), &list);
|
||||
const ssize_t count = libusb_get_device_list(m_libusb_context, &list);
|
||||
if (count < 0)
|
||||
{
|
||||
WARN_LOG(IOS_USB, "Failed to get device list: %s",
|
||||
@ -135,17 +145,25 @@ bool USBHost::AddNewDevices(std::set<u64>& new_devices, DeviceChangeHooks& hooks
|
||||
libusb_get_device_descriptor(device, &descriptor);
|
||||
if (!SConfig::GetInstance().IsUSBDeviceWhitelisted(
|
||||
{descriptor.idVendor, descriptor.idProduct}))
|
||||
{
|
||||
libusb_unref_device(device);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto usb_device = std::make_unique<USB::LibusbDevice>(device, descriptor);
|
||||
if (!ShouldAddDevice(*usb_device))
|
||||
{
|
||||
libusb_unref_device(device);
|
||||
continue;
|
||||
}
|
||||
const u64 id = usb_device->GetId();
|
||||
new_devices.insert(id);
|
||||
if (AddDevice(std::move(usb_device)) || always_add_hooks)
|
||||
hooks.emplace(GetDeviceById(id), ChangeEvent::Inserted);
|
||||
else
|
||||
libusb_unref_device(device);
|
||||
}
|
||||
libusb_free_device_list(list, 1);
|
||||
libusb_free_device_list(list, 0);
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
@ -200,12 +218,11 @@ void USBHost::StartThreads()
|
||||
}
|
||||
|
||||
#ifdef __LIBUSB__
|
||||
if (!m_event_thread_running.IsSet())
|
||||
if (!m_event_thread_running.IsSet() && m_libusb_context)
|
||||
{
|
||||
m_event_thread_running.Set();
|
||||
m_event_thread = std::thread([this] {
|
||||
Common::SetCurrentThreadName("USB Passthrough Thread");
|
||||
std::shared_ptr<libusb_context> context;
|
||||
while (m_event_thread_running.IsSet())
|
||||
{
|
||||
if (SConfig::GetInstance().m_usb_passthrough_devices.empty())
|
||||
@ -214,15 +231,8 @@ void USBHost::StartThreads()
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!context)
|
||||
context = LibusbContext::Get();
|
||||
|
||||
// If we failed to get a libusb context, stop the thread.
|
||||
if (!context)
|
||||
return;
|
||||
|
||||
static timeval tv = {0, 50000};
|
||||
libusb_handle_events_timeout_completed(context.get(), &tv, nullptr);
|
||||
libusb_handle_events_timeout_completed(m_libusb_context, &tv, nullptr);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ class USBHost : public Device
|
||||
{
|
||||
public:
|
||||
USBHost(u32 device_id, const std::string& device_name);
|
||||
virtual ~USBHost() = default;
|
||||
virtual ~USBHost();
|
||||
|
||||
ReturnCode Open(const OpenRequest& request) override;
|
||||
|
||||
@ -71,6 +71,8 @@ private:
|
||||
void DispatchHooks(const DeviceChangeHooks& hooks);
|
||||
|
||||
#ifdef __LIBUSB__
|
||||
libusb_context* m_libusb_context = nullptr;
|
||||
|
||||
// Event thread for libusb
|
||||
Common::Flag m_event_thread_running;
|
||||
std::thread m_event_thread;
|
||||
|
Reference in New Issue
Block a user