Skip to content
Permalink
Browse files

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.
  • Loading branch information...
leoetlino committed May 12, 2019
1 parent 054b117 commit 256c9375c96b4c6ad68b2c50ab714aa66951f41b
@@ -10,6 +10,7 @@ add_library(core
GeckoCodeConfig.cpp
GeckoCode.cpp
HotkeyManager.cpp
LibusbUtils.cpp
MemTools.cpp
Movie.cpp
NetPlayClient.cpp
@@ -231,6 +231,9 @@
<ClCompile Include="IOS\USB\LibusbDevice.cpp">
<DisableSpecificWarnings>4200;%(DisableSpecificWarnings)</DisableSpecificWarnings>
</ClCompile>
<ClCompile Include="LibusbUtils.cpp">
<DisableSpecificWarnings>4200;%(DisableSpecificWarnings)</DisableSpecificWarnings>
</ClCompile>
<ClCompile Include="IOS\USB\Host.cpp">
<DisableSpecificWarnings>4200;%(DisableSpecificWarnings)</DisableSpecificWarnings>
</ClCompile>
@@ -497,6 +500,7 @@
<ClInclude Include="IOS\STM\STM.h" />
<ClInclude Include="IOS\USB\Common.h" />
<ClInclude Include="IOS\USB\LibusbDevice.h" />
<ClInclude Include="LibusbUtils.h" />
<ClInclude Include="IOS\USB\Host.h" />
<ClInclude Include="IOS\USB\OH0\OH0.h" />
<ClInclude Include="IOS\USB\OH0\OH0Device.h" />
@@ -173,6 +173,7 @@
<ClCompile Include="Core.cpp" />
<ClCompile Include="CoreTiming.cpp" />
<ClCompile Include="HotkeyManager.cpp" />
<ClCompile Include="LibusbUtils.cpp" />
<ClCompile Include="MemTools.cpp" />
<ClCompile Include="Movie.cpp" />
<ClCompile Include="NetPlayClient.cpp" />
@@ -939,6 +940,7 @@
<ClInclude Include="CoreTiming.h" />
<ClInclude Include="Host.h" />
<ClInclude Include="HotkeyManager.h" />
<ClInclude Include="LibusbUtils.h" />
<ClInclude Include="MemTools.h" />
<ClInclude Include="Movie.h" />
<ClInclude Include="NetPlayClient.h" />
@@ -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 @@ class BluetoothReal final : public BluetoothBase

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 @@ class BluetoothReal final : public BluetoothBase
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 @@ class USBHost : public Device
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;

0 comments on commit 256c937

Please sign in to comment.
You can’t perform that action at this time.