Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #4408 from leoetlino/usb
IOS: USB support (OH0, USB_VEN, USB_HID)
  • Loading branch information
Parlane committed Feb 6, 2017
2 parents 2835cfd + 56fec3d commit d244597
Show file tree
Hide file tree
Showing 60 changed files with 3,326 additions and 859 deletions.
4 changes: 2 additions & 2 deletions Externals/libusb/libusb_static_2013.vcxproj
Expand Up @@ -52,7 +52,7 @@
<ClCompile Include="libusb\sync.c" />
<ClCompile Include="libusb\os\threads_windows.c" />
<ClCompile Include="libusb\os\windows_nt_common.c" />
<ClCompile Include="libusb\os\windows_winusb.c" />
<ClCompile Include="libusb\os\windows_usbdk.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="msvc\config.h" />
Expand All @@ -65,7 +65,7 @@
<ClInclude Include="libusb\version_nano.h" />
<ClInclude Include="libusb\os\windows_common.h" />
<ClInclude Include="libusb\os\windows_nt_common.h" />
<ClInclude Include="libusb\os\windows_winusb.h" />
<ClInclude Include="libusb\os\windows_usbdk.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
2 changes: 1 addition & 1 deletion Externals/libusb/msvc/libusb_usbdk_static_2015.vcxproj
Expand Up @@ -159,4 +159,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
5 changes: 5 additions & 0 deletions Source/Core/Common/CMakeLists.txt
Expand Up @@ -34,6 +34,11 @@ set(SRCS Analytics.cpp
Crypto/ec.cpp
Logging/LogManager.cpp)

if(LIBUSB_FOUND)
set(LIBS ${LIBS} ${LIBUSB_LIBRARIES})
set(SRCS ${SRCS} LibusbContext.cpp)
endif(LIBUSB_FOUND)

if(ANDROID)
set(SRCS ${SRCS}
Logging/ConsoleListenerDroid.cpp)
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/Common/Common.vcxproj
Expand Up @@ -113,6 +113,7 @@
<ClInclude Include="Hash.h" />
<ClInclude Include="IniFile.h" />
<ClInclude Include="JitRegister.h" />
<ClInclude Include="LibusbContext.h" />
<ClInclude Include="LinearDiskCache.h" />
<ClInclude Include="MathUtil.h" />
<ClInclude Include="MD5.h" />
Expand Down Expand Up @@ -159,6 +160,9 @@
<ClCompile Include="Hash.cpp" />
<ClCompile Include="IniFile.cpp" />
<ClCompile Include="JitRegister.cpp" />
<ClCompile Include="LibusbContext.cpp">
<DisableSpecificWarnings>4200;%(DisableSpecificWarnings)</DisableSpecificWarnings>
</ClCompile>
<ClCompile Include="Logging\ConsoleListenerWin.cpp" />
<ClCompile Include="MathUtil.cpp" />
<ClCompile Include="MD5.cpp" />
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/Common/Common.vcxproj.filters
Expand Up @@ -45,6 +45,7 @@
<ClInclude Include="FPURoundMode.h" />
<ClInclude Include="Hash.h" />
<ClInclude Include="IniFile.h" />
<ClInclude Include="LibusbContext.h" />
<ClInclude Include="LinearDiskCache.h" />
<ClInclude Include="MathUtil.h" />
<ClInclude Include="MemArena.h" />
Expand Down Expand Up @@ -236,6 +237,7 @@
<ClCompile Include="FileUtil.cpp" />
<ClCompile Include="Hash.cpp" />
<ClCompile Include="IniFile.cpp" />
<ClCompile Include="LibusbContext.cpp" />
<ClCompile Include="MathUtil.cpp" />
<ClCompile Include="MemArena.cpp" />
<ClCompile Include="MemoryUtil.cpp" />
Expand Down
46 changes: 46 additions & 0 deletions Source/Core/Common/LibusbContext.cpp
@@ -0,0 +1,46 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <libusb.h>
#include <memory>
#include <mutex>

#include "Common/LibusbContext.h"
#include "Common/MsgHandler.h"

namespace LibusbContext
{
static std::shared_ptr<libusb_context> s_libusb_context;
static std::once_flag s_context_initialized;

static libusb_context* Create()
{
libusb_context* context;
const int ret = libusb_init(&context);
if (ret < LIBUSB_SUCCESS)
{
bool is_windows = false;
#ifdef _WIN32
is_windows = true;
#endif
if (is_windows && ret == LIBUSB_ERROR_NOT_FOUND)
PanicAlertT("Failed to initialize libusb because usbdk is not installed.");
else
PanicAlertT("Failed to initialize libusb: %s", libusb_error_name(ret));
return nullptr;
}
return context;
}

std::shared_ptr<libusb_context> Get()
{
std::call_once(s_context_initialized, []() {
s_libusb_context.reset(Create(), [](auto* context) {
if (context != nullptr)
libusb_exit(context);
});
});
return s_libusb_context;
}
}
15 changes: 15 additions & 0 deletions Source/Core/Common/LibusbContext.h
@@ -0,0 +1,15 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <memory>

struct libusb_context;

namespace LibusbContext
{
// libusb on Windows is limited to only a single context. Trying to open more
// than one can cause issues with device enumerations.
// libusb is thread-safe so this context can be safely used from different threads.
std::shared_ptr<libusb_context> Get();
}
1 change: 0 additions & 1 deletion Source/Core/Common/Logging/Log.h
Expand Up @@ -31,7 +31,6 @@ enum LOG_TYPE
IOS_DI,
IOS_ES,
IOS_FILEIO,
IOS_HID,
IOS_NET,
IOS_SD,
IOS_SSL,
Expand Down
1 change: 0 additions & 1 deletion Source/Core/Common/Logging/LogManager.cpp
Expand Up @@ -65,7 +65,6 @@ LogManager::LogManager()
m_Log[LogTypes::IOS_DI] = new LogContainer("IOS_DI", "IOS - Drive Interface");
m_Log[LogTypes::IOS_ES] = new LogContainer("IOS_ES", "IOS - ETicket Services");
m_Log[LogTypes::IOS_FILEIO] = new LogContainer("IOS_FILEIO", "IOS - FileIO");
m_Log[LogTypes::IOS_HID] = new LogContainer("IOS_HID", "IOS - USB_HID");
m_Log[LogTypes::IOS_SD] = new LogContainer("IOS_SD", "IOS - SDIO");
m_Log[LogTypes::IOS_SSL] = new LogContainer("IOS_SSL", "IOS - SSL");
m_Log[LogTypes::IOS_STM] = new LogContainer("IOS_STM", "IOS - State Transition Manager");
Expand Down
10 changes: 8 additions & 2 deletions Source/Core/Core/CMakeLists.txt
Expand Up @@ -154,9 +154,15 @@ set(SRCS ActionReplay.cpp
IOS/SDIO/SDIOSlot0.cpp
IOS/STM/STM.cpp
IOS/USB/Common.cpp
IOS/USB/Host.cpp
IOS/USB/OH0/OH0.cpp
IOS/USB/OH0/OH0Device.cpp
IOS/USB/USB_HID/HIDv4.cpp
IOS/USB/USB_VEN/VEN.cpp
IOS/USB/USBV0.cpp
IOS/USB/USBV4.cpp
IOS/USB/USBV5.cpp
IOS/USB/USB_KBD.cpp
IOS/USB/USB_VEN.cpp
IOS/USB/Bluetooth/BTBase.cpp
IOS/USB/Bluetooth/BTEmu.cpp
IOS/USB/Bluetooth/BTStub.cpp
Expand Down Expand Up @@ -264,7 +270,7 @@ set(LIBS
if(LIBUSB_FOUND)
# Using shared LibUSB
set(LIBS ${LIBS} ${LIBUSB_LIBRARIES})
set(SRCS ${SRCS} IOS/USB/USB_HIDv4.cpp
set(SRCS ${SRCS} IOS/USB/LibusbDevice.cpp
IOS/USB/Bluetooth/BTReal.cpp)
endif()

Expand Down
42 changes: 42 additions & 0 deletions Source/Core/Core/ConfigManager.cpp
Expand Up @@ -77,6 +77,7 @@ void SConfig::SaveSettings()
SaveAnalyticsSettings(ini);
SaveNetworkSettings(ini);
SaveBluetoothPassthroughSettings(ini);
SaveUSBPassthroughSettings(ini);
SaveSysconfSettings(ini);

ini.Save(File::GetUserPath(F_DOLPHINCONFIG_IDX));
Expand Down Expand Up @@ -343,6 +344,20 @@ void SConfig::SaveBluetoothPassthroughSettings(IniFile& ini)
section->Set("LinkKeys", m_bt_passthrough_link_keys);
}

void SConfig::SaveUSBPassthroughSettings(IniFile& ini)
{
IniFile::Section* section = ini.GetOrCreateSection("USBPassthrough");

std::ostringstream oss;
for (const auto& device : m_usb_passthrough_devices)
oss << StringFromFormat("%04x:%04x", device.first, device.second) << ',';
std::string devices_string = oss.str();
if (!devices_string.empty())
devices_string.pop_back();

section->Set("Devices", devices_string);
}

void SConfig::SaveSysconfSettings(IniFile& ini)
{
IniFile::Section* section = ini.GetOrCreateSection("Sysconf");
Expand Down Expand Up @@ -400,6 +415,7 @@ void SConfig::LoadSettings()
LoadNetworkSettings(ini);
LoadAnalyticsSettings(ini);
LoadBluetoothPassthroughSettings(ini);
LoadUSBPassthroughSettings(ini);
LoadSysconfSettings(ini);
}

Expand Down Expand Up @@ -666,6 +682,27 @@ void SConfig::LoadBluetoothPassthroughSettings(IniFile& ini)
section->Get("LinkKeys", &m_bt_passthrough_link_keys, "");
}

void SConfig::LoadUSBPassthroughSettings(IniFile& ini)
{
IniFile::Section* section = ini.GetOrCreateSection("USBPassthrough");
m_usb_passthrough_devices.clear();
std::string devices_string;
std::vector<std::string> pairs;
section->Get("Devices", &devices_string, "");
SplitString(devices_string, ',', pairs);
for (const auto& pair : pairs)
{
const auto index = pair.find(':');
if (index == std::string::npos)
continue;

const u16 vid = static_cast<u16>(strtol(pair.substr(0, index).c_str(), nullptr, 16));
const u16 pid = static_cast<u16>(strtol(pair.substr(index + 1).c_str(), nullptr, 16));
if (vid && pid)
m_usb_passthrough_devices.emplace(vid, pid);
}
}

void SConfig::LoadSysconfSettings(IniFile& ini)
{
IniFile::Section* section = ini.GetOrCreateSection("Sysconf");
Expand Down Expand Up @@ -757,6 +794,11 @@ void SConfig::LoadDefaults()
m_revision = 0;
}

bool SConfig::IsUSBDeviceWhitelisted(const std::pair<u16, u16> vid_pid) const
{
return m_usb_passthrough_devices.find(vid_pid) != m_usb_passthrough_devices.end();
}

const char* SConfig::GetDirectoryForRegion(DiscIO::Region region)
{
switch (region)
Expand Down
8 changes: 8 additions & 0 deletions Source/Core/Core/ConfigManager.h
Expand Up @@ -5,7 +5,9 @@
#pragma once

#include <limits>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "Common/IniFile.h"
Expand Down Expand Up @@ -151,6 +153,10 @@ struct SConfig : NonCopyable
int m_bt_passthrough_vid = -1;
std::string m_bt_passthrough_link_keys;

// USB passthrough settings
std::set<std::pair<u16, u16>> m_usb_passthrough_devices;
bool IsUSBDeviceWhitelisted(std::pair<u16, u16> vid_pid) const;

// SYSCONF settings
int m_sensor_bar_position = 0x01;
int m_sensor_bar_sensitivity = 0x03;
Expand Down Expand Up @@ -351,6 +357,7 @@ struct SConfig : NonCopyable
void SaveNetworkSettings(IniFile& ini);
void SaveAnalyticsSettings(IniFile& ini);
void SaveBluetoothPassthroughSettings(IniFile& ini);
void SaveUSBPassthroughSettings(IniFile& ini);
void SaveSysconfSettings(IniFile& ini);

void LoadGeneralSettings(IniFile& ini);
Expand All @@ -365,6 +372,7 @@ struct SConfig : NonCopyable
void LoadNetworkSettings(IniFile& ini);
void LoadAnalyticsSettings(IniFile& ini);
void LoadBluetoothPassthroughSettings(IniFile& ini);
void LoadUSBPassthroughSettings(IniFile& ini);
void LoadSysconfSettings(IniFile& ini);

bool SetRegion(DiscIO::Region region, std::string* directory_name);
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/Core.cpp
Expand Up @@ -54,7 +54,7 @@
#include "Core/HW/SystemTimers.h"
#include "Core/HW/VideoInterface.h"
#include "Core/HW/Wiimote.h"
#include "Core/IOS/Network/Socket.h"
#include "Core/IOS/IPC.h"
#include "Core/IOS/USB/Bluetooth/BTEmu.h"
#include "Core/IOS/USB/Bluetooth/WiimoteDevice.h"
#include "Core/Movie.h"
Expand Down Expand Up @@ -975,7 +975,7 @@ void UpdateWantDeterminism(bool initial)
bool was_unpaused = Core::PauseAndLock(true);

g_want_determinism = new_want_determinism;
IOS::HLE::WiiSockMan::GetInstance().UpdateWantDeterminism(new_want_determinism);
IOS::HLE::UpdateWantDeterminism(new_want_determinism);
Fifo::UpdateWantDeterminism(new_want_determinism);
// We need to clear the cache because some parts of the JIT depend on want_determinism, e.g. use
// of FMA.
Expand Down
28 changes: 19 additions & 9 deletions Source/Core/Core/Core.vcxproj
Expand Up @@ -186,16 +186,20 @@
<ClCompile Include="IOS\SDIO\SDIOSlot0.cpp" />
<ClCompile Include="IOS\STM\STM.cpp" />
<ClCompile Include="IOS\USB\Common.cpp" />
<ClCompile Include="IOS\USB\USBV0.cpp" />
<ClCompile Include="IOS\USB\USB_HIDv4.cpp">
<!--
Disable "nonstandard extension used : zero-sized array in struct/union" warning,
which is hit in libusb.h.
-->
<ClCompile Include="IOS\USB\LibusbDevice.cpp">
<DisableSpecificWarnings>4200;%(DisableSpecificWarnings)</DisableSpecificWarnings>
</ClCompile>
<ClCompile Include="IOS\USB\Host.cpp">
<DisableSpecificWarnings>4200;%(DisableSpecificWarnings)</DisableSpecificWarnings>
</ClCompile>
<ClCompile Include="IOS\USB\OH0\OH0.cpp" />
<ClCompile Include="IOS\USB\OH0\OH0Device.cpp" />
<ClCompile Include="IOS\USB\USB_HID\HIDv4.cpp" />
<ClCompile Include="IOS\USB\USB_VEN\VEN.cpp" />
<ClCompile Include="IOS\USB\USBV0.cpp" />
<ClCompile Include="IOS\USB\USBV4.cpp" />
<ClCompile Include="IOS\USB\USBV5.cpp" />
<ClCompile Include="IOS\USB\USB_KBD.cpp" />
<ClCompile Include="IOS\USB\USB_VEN.cpp" />
<ClCompile Include="IOS\USB\Bluetooth\BTBase.cpp" />
<ClCompile Include="IOS\USB\Bluetooth\BTEmu.cpp" />
<ClCompile Include="IOS\USB\Bluetooth\BTStub.cpp" />
Expand Down Expand Up @@ -419,10 +423,16 @@
<ClInclude Include="IOS\SDIO\SDIOSlot0.h" />
<ClInclude Include="IOS\STM\STM.h" />
<ClInclude Include="IOS\USB\Common.h" />
<ClInclude Include="IOS\USB\LibusbDevice.h" />
<ClInclude Include="IOS\USB\Host.h" />
<ClInclude Include="IOS\USB\OH0\OH0.h" />
<ClInclude Include="IOS\USB\OH0\OH0Device.h" />
<ClInclude Include="IOS\USB\USB_HID\HIDv4.h" />
<ClInclude Include="IOS\USB\USB_VEN\VEN.h" />
<ClInclude Include="IOS\USB\USBV0.h" />
<ClInclude Include="IOS\USB\USB_HIDv4.h" />
<ClInclude Include="IOS\USB\USBV4.h" />
<ClInclude Include="IOS\USB\USBV5.h" />
<ClInclude Include="IOS\USB\USB_KBD.h" />
<ClInclude Include="IOS\USB\USB_VEN.h" />
<ClInclude Include="IOS\USB\Bluetooth\BTBase.h" />
<ClInclude Include="IOS\USB\Bluetooth\BTEmu.h" />
<ClInclude Include="IOS\USB\Bluetooth\BTStub.h" />
Expand Down

0 comments on commit d244597

Please sign in to comment.