mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-29 17:19:44 -06:00
Move libusb utilities to LibusbUtils
* Simplifies libusb context usage and allows us to set options for all contexts easily. Notably, this lets us enable usbdk support in libusb, which is now opt-in in the latest version. * Moves the libusb config descriptor wrapper class to LibusbUtils too since that could easily be reused. * Moves device listing to LibusbUtils too and add a lock around it as some libusb backends are not thread safe. * Consequences: only a single context and a single event handling thread is used now, which is more efficient.
This commit is contained in:
@ -24,11 +24,11 @@
|
||||
#include "Common/Network.h"
|
||||
#include "Common/StringUtil.h"
|
||||
#include "Common/Swap.h"
|
||||
#include "Common/Thread.h"
|
||||
#include "Core/ConfigManager.h"
|
||||
#include "Core/Core.h"
|
||||
#include "Core/HW/Memmap.h"
|
||||
#include "Core/IOS/Device.h"
|
||||
#include "Core/LibusbUtils.h"
|
||||
#include "VideoCommon/OnScreenDisplay.h"
|
||||
|
||||
namespace IOS::HLE::Device
|
||||
@ -57,11 +57,6 @@ static bool IsBluetoothDevice(const libusb_interface_descriptor& descriptor)
|
||||
BluetoothReal::BluetoothReal(Kernel& ios, const std::string& device_name)
|
||||
: BluetoothBase(ios, device_name)
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
@ -72,42 +67,27 @@ BluetoothReal::~BluetoothReal()
|
||||
SendHCIResetCommand();
|
||||
WaitForHCICommandComplete(HCI_CMD_RESET);
|
||||
libusb_release_interface(m_handle, 0);
|
||||
// libusb_handle_events() may block the libusb thread indefinitely, so we need to
|
||||
// call libusb_close() first then immediately stop the thread in StopTransferThread.
|
||||
StopTransferThread();
|
||||
libusb_close(m_handle);
|
||||
libusb_unref_device(m_device);
|
||||
}
|
||||
|
||||
libusb_exit(m_libusb_context);
|
||||
SaveLinkKeys();
|
||||
}
|
||||
|
||||
IPCCommandResult BluetoothReal::Open(const OpenRequest& request)
|
||||
{
|
||||
if (!m_libusb_context)
|
||||
auto& context = LibusbUtils::GetContext();
|
||||
if (!context.IsValid())
|
||||
return GetDefaultReply(IPC_EACCES);
|
||||
|
||||
libusb_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 GetDefaultReply(IPC_ENOENT);
|
||||
}
|
||||
|
||||
for (ssize_t i = 0; i < cnt; ++i)
|
||||
{
|
||||
libusb_device* device = list[i];
|
||||
context.GetDeviceList([this](libusb_device* device) {
|
||||
libusb_device_descriptor device_descriptor;
|
||||
libusb_config_descriptor* config_descriptor;
|
||||
libusb_get_device_descriptor(device, &device_descriptor);
|
||||
const int ret = libusb_get_config_descriptor(device, 0, &config_descriptor);
|
||||
if (ret != 0)
|
||||
auto config_descriptor = LibusbUtils::MakeConfigDescriptor(device);
|
||||
if (!config_descriptor)
|
||||
{
|
||||
ERROR_LOG(IOS_WIIMOTE, "Failed to get config descriptor for device %04x:%04x: %s",
|
||||
device_descriptor.idVendor, device_descriptor.idProduct, libusb_error_name(ret));
|
||||
continue;
|
||||
ERROR_LOG(IOS_WIIMOTE, "Failed to get config descriptor for device %04x:%04x",
|
||||
device_descriptor.idVendor, device_descriptor.idProduct);
|
||||
return true;
|
||||
}
|
||||
|
||||
const libusb_interface& interface = config_descriptor->interface[INTERFACE];
|
||||
@ -126,12 +106,10 @@ IPCCommandResult BluetoothReal::Open(const OpenRequest& request)
|
||||
device_descriptor.bcdDevice, manufacturer, product, serial_number);
|
||||
m_is_wii_bt_module =
|
||||
device_descriptor.idVendor == 0x57e && device_descriptor.idProduct == 0x305;
|
||||
libusb_free_config_descriptor(config_descriptor);
|
||||
break;
|
||||
return false;
|
||||
}
|
||||
libusb_free_config_descriptor(config_descriptor);
|
||||
}
|
||||
libusb_free_device_list(list, 1);
|
||||
return true;
|
||||
});
|
||||
|
||||
if (m_handle == nullptr)
|
||||
{
|
||||
@ -141,8 +119,6 @@ IPCCommandResult BluetoothReal::Open(const OpenRequest& request)
|
||||
return GetDefaultReply(IPC_ENOENT);
|
||||
}
|
||||
|
||||
StartTransferThread();
|
||||
|
||||
return Device::Open(request);
|
||||
}
|
||||
|
||||
@ -151,7 +127,7 @@ IPCCommandResult BluetoothReal::Close(u32 fd)
|
||||
if (m_handle)
|
||||
{
|
||||
libusb_release_interface(m_handle, 0);
|
||||
StopTransferThread();
|
||||
libusb_close(m_handle);
|
||||
libusb_unref_device(m_device);
|
||||
m_handle = nullptr;
|
||||
}
|
||||
@ -595,32 +571,6 @@ bool BluetoothReal::OpenDevice(libusb_device* device)
|
||||
return true;
|
||||
}
|
||||
|
||||
void BluetoothReal::StartTransferThread()
|
||||
{
|
||||
if (m_thread_running.IsSet())
|
||||
return;
|
||||
m_thread_running.Set();
|
||||
m_thread = std::thread(&BluetoothReal::TransferThread, this);
|
||||
}
|
||||
|
||||
void BluetoothReal::StopTransferThread()
|
||||
{
|
||||
if (m_thread_running.TestAndClear())
|
||||
{
|
||||
libusb_close(m_handle);
|
||||
m_thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
void BluetoothReal::TransferThread()
|
||||
{
|
||||
Common::SetCurrentThreadName("BT USB Thread");
|
||||
while (m_thread_running.IsSet())
|
||||
{
|
||||
libusb_handle_events_completed(m_libusb_context, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
// The callbacks are called from libusb code on a separate thread.
|
||||
void BluetoothReal::HandleCtrlTransfer(libusb_transfer* tr)
|
||||
{
|
||||
|
@ -11,7 +11,6 @@
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#include "Common/CommonTypes.h"
|
||||
#include "Common/Flag.h"
|
||||
@ -22,7 +21,6 @@
|
||||
#include "Core/IOS/USB/USBV0.h"
|
||||
|
||||
class PointerWrap;
|
||||
struct libusb_context;
|
||||
struct libusb_device;
|
||||
struct libusb_device_handle;
|
||||
struct libusb_transfer;
|
||||
@ -74,10 +72,6 @@ private:
|
||||
|
||||
libusb_device* m_device = nullptr;
|
||||
libusb_device_handle* m_handle = nullptr;
|
||||
libusb_context* m_libusb_context = nullptr;
|
||||
|
||||
Common::Flag m_thread_running;
|
||||
std::thread m_thread;
|
||||
|
||||
std::mutex m_transfers_mutex;
|
||||
struct PendingTransfer
|
||||
@ -122,9 +116,6 @@ private:
|
||||
void SaveLinkKeys();
|
||||
|
||||
bool OpenDevice(libusb_device* device);
|
||||
void StartTransferThread();
|
||||
void StopTransferThread();
|
||||
void TransferThread();
|
||||
};
|
||||
} // namespace Device
|
||||
} // namespace IOS::HLE
|
||||
|
@ -24,24 +24,15 @@
|
||||
#include "Core/Core.h"
|
||||
#include "Core/IOS/USB/Common.h"
|
||||
#include "Core/IOS/USB/LibusbDevice.h"
|
||||
#include "Core/LibusbUtils.h"
|
||||
|
||||
namespace IOS::HLE::Device
|
||||
{
|
||||
USBHost::USBHost(Kernel& ios, const std::string& device_name) : Device(ios, device_name)
|
||||
{
|
||||
#ifdef __LIBUSB__
|
||||
const int ret = libusb_init(&m_libusb_context);
|
||||
DEBUG_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
|
||||
}
|
||||
USBHost::~USBHost() = default;
|
||||
|
||||
IPCCommandResult USBHost::Open(const OpenRequest& request)
|
||||
{
|
||||
@ -130,43 +121,26 @@ bool USBHost::AddNewDevices(std::set<u64>& new_devices, DeviceChangeHooks& hooks
|
||||
if (SConfig::GetInstance().m_usb_passthrough_devices.empty())
|
||||
return true;
|
||||
|
||||
if (m_libusb_context)
|
||||
auto& context = LibusbUtils::GetContext();
|
||||
if (context.IsValid())
|
||||
{
|
||||
libusb_device** 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",
|
||||
libusb_error_name(static_cast<int>(count)));
|
||||
return false;
|
||||
}
|
||||
|
||||
for (ssize_t i = 0; i < count; ++i)
|
||||
{
|
||||
libusb_device* device = list[i];
|
||||
context.GetDeviceList([&](libusb_device* device) {
|
||||
libusb_device_descriptor descriptor;
|
||||
libusb_get_device_descriptor(device, &descriptor);
|
||||
if (!SConfig::GetInstance().IsUSBDeviceWhitelisted(
|
||||
{descriptor.idVendor, descriptor.idProduct}))
|
||||
{
|
||||
libusb_unref_device(device);
|
||||
continue;
|
||||
}
|
||||
const std::pair<u16, u16> vid_pid = {descriptor.idVendor, descriptor.idProduct};
|
||||
if (!SConfig::GetInstance().IsUSBDeviceWhitelisted(vid_pid))
|
||||
return true;
|
||||
|
||||
auto usb_device = std::make_unique<USB::LibusbDevice>(m_ios, device, descriptor);
|
||||
if (!ShouldAddDevice(*usb_device))
|
||||
{
|
||||
libusb_unref_device(device);
|
||||
continue;
|
||||
}
|
||||
return true;
|
||||
|
||||
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, 0);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
@ -219,27 +193,6 @@ void USBHost::StartThreads()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#ifdef __LIBUSB__
|
||||
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");
|
||||
while (m_event_thread_running.IsSet())
|
||||
{
|
||||
if (SConfig::GetInstance().m_usb_passthrough_devices.empty())
|
||||
{
|
||||
Common::SleepCurrentThread(50);
|
||||
continue;
|
||||
}
|
||||
|
||||
static timeval tv = {0, 50000};
|
||||
libusb_handle_events_timeout_completed(m_libusb_context, &tv, nullptr);
|
||||
}
|
||||
});
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void USBHost::StopThreads()
|
||||
@ -251,10 +204,6 @@ void USBHost::StopThreads()
|
||||
DeviceChangeHooks hooks;
|
||||
DetectRemovedDevices(std::set<u64>(), hooks);
|
||||
DispatchHooks(hooks);
|
||||
#ifdef __LIBUSB__
|
||||
if (m_event_thread_running.TestAndClear())
|
||||
m_event_thread.join();
|
||||
#endif
|
||||
}
|
||||
|
||||
IPCCommandResult USBHost::HandleTransfer(std::shared_ptr<USB::Device> device, u32 request,
|
||||
|
@ -22,7 +22,6 @@
|
||||
#include "Core/IOS/USB/Common.h"
|
||||
|
||||
class PointerWrap;
|
||||
struct libusb_context;
|
||||
|
||||
namespace IOS::HLE::Device
|
||||
{
|
||||
@ -67,13 +66,6 @@ private:
|
||||
void DetectRemovedDevices(const std::set<u64>& plugged_devices, DeviceChangeHooks& hooks);
|
||||
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;
|
||||
#endif
|
||||
// Device scanning thread
|
||||
Common::Flag m_scan_thread_running;
|
||||
std::thread m_scan_thread;
|
||||
|
@ -37,7 +37,7 @@ LibusbDevice::LibusbDevice(Kernel& ios, libusb_device* device,
|
||||
static_cast<u64>(libusb_get_device_address(device)));
|
||||
|
||||
for (u8 i = 0; i < descriptor.bNumConfigurations; ++i)
|
||||
m_config_descriptors.emplace_back(std::make_unique<LibusbConfigDescriptor>(m_device, i));
|
||||
m_config_descriptors.emplace_back(LibusbUtils::MakeConfigDescriptor(m_device, i));
|
||||
}
|
||||
|
||||
LibusbDevice::~LibusbDevice()
|
||||
@ -65,13 +65,13 @@ std::vector<ConfigDescriptor> LibusbDevice::GetConfigurations() const
|
||||
std::vector<ConfigDescriptor> descriptors;
|
||||
for (const auto& config_descriptor : m_config_descriptors)
|
||||
{
|
||||
if (!config_descriptor->IsValid())
|
||||
if (!config_descriptor)
|
||||
{
|
||||
ERROR_LOG(IOS_USB, "Ignoring invalid config descriptor for %04x:%04x", m_vid, m_pid);
|
||||
continue;
|
||||
}
|
||||
ConfigDescriptor descriptor;
|
||||
std::memcpy(&descriptor, config_descriptor->Get(), sizeof(descriptor));
|
||||
std::memcpy(&descriptor, config_descriptor.get(), sizeof(descriptor));
|
||||
descriptors.push_back(descriptor);
|
||||
}
|
||||
return descriptors;
|
||||
@ -80,14 +80,14 @@ std::vector<ConfigDescriptor> LibusbDevice::GetConfigurations() const
|
||||
std::vector<InterfaceDescriptor> LibusbDevice::GetInterfaces(const u8 config) const
|
||||
{
|
||||
std::vector<InterfaceDescriptor> descriptors;
|
||||
if (config >= m_config_descriptors.size() || !m_config_descriptors[config]->IsValid())
|
||||
if (config >= m_config_descriptors.size() || !m_config_descriptors[config])
|
||||
{
|
||||
ERROR_LOG(IOS_USB, "Invalid config descriptor %u for %04x:%04x", config, m_vid, m_pid);
|
||||
return descriptors;
|
||||
}
|
||||
for (u8 i = 0; i < m_config_descriptors[config]->Get()->bNumInterfaces; ++i)
|
||||
for (u8 i = 0; i < m_config_descriptors[config]->bNumInterfaces; ++i)
|
||||
{
|
||||
const libusb_interface& interface = m_config_descriptors[config]->Get()->interface[i];
|
||||
const libusb_interface& interface = m_config_descriptors[config]->interface[i];
|
||||
for (u8 a = 0; a < interface.num_altsetting; ++a)
|
||||
{
|
||||
InterfaceDescriptor descriptor;
|
||||
@ -102,13 +102,13 @@ std::vector<EndpointDescriptor>
|
||||
LibusbDevice::GetEndpoints(const u8 config, const u8 interface_number, const u8 alt_setting) const
|
||||
{
|
||||
std::vector<EndpointDescriptor> descriptors;
|
||||
if (config >= m_config_descriptors.size() || !m_config_descriptors[config]->IsValid())
|
||||
if (config >= m_config_descriptors.size() || !m_config_descriptors[config])
|
||||
{
|
||||
ERROR_LOG(IOS_USB, "Invalid config descriptor %u for %04x:%04x", config, m_vid, m_pid);
|
||||
return descriptors;
|
||||
}
|
||||
ASSERT(interface_number < m_config_descriptors[config]->Get()->bNumInterfaces);
|
||||
const auto& interface = m_config_descriptors[config]->Get()->interface[interface_number];
|
||||
ASSERT(interface_number < m_config_descriptors[config]->bNumInterfaces);
|
||||
const auto& interface = m_config_descriptors[config]->interface[interface_number];
|
||||
ASSERT(alt_setting < interface.num_altsetting);
|
||||
const libusb_interface_descriptor& interface_descriptor = interface.altsetting[alt_setting];
|
||||
for (u8 i = 0; i < interface_descriptor.bNumEndpoints; ++i)
|
||||
@ -403,16 +403,16 @@ void LibusbDevice::TransferEndpoint::CancelTransfers()
|
||||
|
||||
int LibusbDevice::GetNumberOfAltSettings(const u8 interface_number)
|
||||
{
|
||||
return m_config_descriptors[0]->Get()->interface[interface_number].num_altsetting;
|
||||
return m_config_descriptors[0]->interface[interface_number].num_altsetting;
|
||||
}
|
||||
|
||||
template <typename Configs, typename Function>
|
||||
static int DoForEachInterface(const Configs& configs, u8 config_num, Function action)
|
||||
{
|
||||
int ret = LIBUSB_ERROR_NOT_FOUND;
|
||||
if (configs.size() <= config_num || !configs[config_num]->IsValid())
|
||||
if (configs.size() <= config_num || !configs[config_num])
|
||||
return ret;
|
||||
for (u8 i = 0; i < configs[config_num]->Get()->bNumInterfaces; ++i)
|
||||
for (u8 i = 0; i < configs[config_num]->bNumInterfaces; ++i)
|
||||
{
|
||||
ret = action(i);
|
||||
if (ret < 0)
|
||||
@ -462,16 +462,4 @@ int LibusbDevice::ReleaseAllInterfacesForCurrentConfig() const
|
||||
return get_config_ret;
|
||||
return ReleaseAllInterfaces(config_num);
|
||||
}
|
||||
|
||||
LibusbConfigDescriptor::LibusbConfigDescriptor(libusb_device* device, const u8 config_num)
|
||||
{
|
||||
if (libusb_get_config_descriptor(device, config_num, &m_descriptor) != LIBUSB_SUCCESS)
|
||||
m_descriptor = nullptr;
|
||||
}
|
||||
|
||||
LibusbConfigDescriptor::~LibusbConfigDescriptor()
|
||||
{
|
||||
if (m_descriptor != nullptr)
|
||||
libusb_free_config_descriptor(m_descriptor);
|
||||
}
|
||||
} // namespace IOS::HLE::USB
|
||||
|
@ -15,8 +15,8 @@
|
||||
|
||||
#include "Common/CommonTypes.h"
|
||||
#include "Core/IOS/USB/Common.h"
|
||||
#include "Core/LibusbUtils.h"
|
||||
|
||||
struct libusb_config_descriptor;
|
||||
struct libusb_device;
|
||||
struct libusb_device_descriptor;
|
||||
struct libusb_device_handle;
|
||||
@ -24,19 +24,6 @@ struct libusb_transfer;
|
||||
|
||||
namespace IOS::HLE::USB
|
||||
{
|
||||
// Simple wrapper around libusb_get_config_descriptor and libusb_free_config_descriptor.
|
||||
class LibusbConfigDescriptor final
|
||||
{
|
||||
public:
|
||||
explicit LibusbConfigDescriptor(libusb_device* device, u8 config_num = 0);
|
||||
~LibusbConfigDescriptor();
|
||||
libusb_config_descriptor* Get() const { return m_descriptor; }
|
||||
bool IsValid() const { return m_descriptor != nullptr; }
|
||||
|
||||
private:
|
||||
libusb_config_descriptor* m_descriptor = nullptr;
|
||||
};
|
||||
|
||||
class LibusbDevice final : public Device
|
||||
{
|
||||
public:
|
||||
@ -62,7 +49,7 @@ public:
|
||||
private:
|
||||
Kernel& m_ios;
|
||||
|
||||
std::vector<std::unique_ptr<LibusbConfigDescriptor>> m_config_descriptors;
|
||||
std::vector<LibusbUtils::ConfigDescriptor> m_config_descriptors;
|
||||
u16 m_vid = 0;
|
||||
u16 m_pid = 0;
|
||||
u8 m_active_interface = 0;
|
||||
|
Reference in New Issue
Block a user