hid: avoid direct pointer access of transfer memory objects

master
Liam 2023-02-24 12:29:55 +07:00
parent ca8a804a3c
commit de4e5db330
20 changed files with 91 additions and 69 deletions

@ -1,17 +1,18 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#include "core/core.h"
#include "core/core_timing.h" #include "core/core_timing.h"
#include "core/hid/emulated_console.h" #include "core/hid/emulated_console.h"
#include "core/hid/hid_core.h" #include "core/hid/hid_core.h"
#include "core/hle/service/hid/controllers/console_sixaxis.h" #include "core/hle/service/hid/controllers/console_sixaxis.h"
#include "core/memory.h"
namespace Service::HID { namespace Service::HID {
constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3C200; constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3C200;
Controller_ConsoleSixAxis::Controller_ConsoleSixAxis(Core::HID::HIDCore& hid_core_, Controller_ConsoleSixAxis::Controller_ConsoleSixAxis(Core::System& system_, u8* raw_shared_memory_)
u8* raw_shared_memory_) : ControllerBase{system_.HIDCore()}, system{system_} {
: ControllerBase{hid_core_} {
console = hid_core.GetEmulatedConsole(); console = hid_core.GetEmulatedConsole();
static_assert(SHARED_MEMORY_OFFSET + sizeof(ConsoleSharedMemory) < shared_memory_size, static_assert(SHARED_MEMORY_OFFSET + sizeof(ConsoleSharedMemory) < shared_memory_size,
"ConsoleSharedMemory is bigger than the shared memory"); "ConsoleSharedMemory is bigger than the shared memory");
@ -26,7 +27,7 @@ void Controller_ConsoleSixAxis::OnInit() {}
void Controller_ConsoleSixAxis::OnRelease() {} void Controller_ConsoleSixAxis::OnRelease() {}
void Controller_ConsoleSixAxis::OnUpdate(const Core::Timing::CoreTiming& core_timing) { void Controller_ConsoleSixAxis::OnUpdate(const Core::Timing::CoreTiming& core_timing) {
if (!IsControllerActivated() || !is_transfer_memory_set) { if (!IsControllerActivated() || transfer_memory == 0) {
seven_sixaxis_lifo.buffer_count = 0; seven_sixaxis_lifo.buffer_count = 0;
seven_sixaxis_lifo.buffer_tail = 0; seven_sixaxis_lifo.buffer_tail = 0;
return; return;
@ -59,11 +60,10 @@ void Controller_ConsoleSixAxis::OnUpdate(const Core::Timing::CoreTiming& core_ti
// Update seven six axis transfer memory // Update seven six axis transfer memory
seven_sixaxis_lifo.WriteNextEntry(next_seven_sixaxis_state); seven_sixaxis_lifo.WriteNextEntry(next_seven_sixaxis_state);
std::memcpy(transfer_memory, &seven_sixaxis_lifo, sizeof(seven_sixaxis_lifo)); system.Memory().WriteBlock(transfer_memory, &seven_sixaxis_lifo, sizeof(seven_sixaxis_lifo));
} }
void Controller_ConsoleSixAxis::SetTransferMemoryPointer(u8* t_mem) { void Controller_ConsoleSixAxis::SetTransferMemoryAddress(VAddr t_mem) {
is_transfer_memory_set = true;
transfer_memory = t_mem; transfer_memory = t_mem;
} }

@ -10,6 +10,10 @@
#include "core/hle/service/hid/controllers/controller_base.h" #include "core/hle/service/hid/controllers/controller_base.h"
#include "core/hle/service/hid/ring_lifo.h" #include "core/hle/service/hid/ring_lifo.h"
namespace Core {
class System;
} // namespace Core
namespace Core::HID { namespace Core::HID {
class EmulatedConsole; class EmulatedConsole;
} // namespace Core::HID } // namespace Core::HID
@ -17,7 +21,7 @@ class EmulatedConsole;
namespace Service::HID { namespace Service::HID {
class Controller_ConsoleSixAxis final : public ControllerBase { class Controller_ConsoleSixAxis final : public ControllerBase {
public: public:
explicit Controller_ConsoleSixAxis(Core::HID::HIDCore& hid_core_, u8* raw_shared_memory_); explicit Controller_ConsoleSixAxis(Core::System& system_, u8* raw_shared_memory_);
~Controller_ConsoleSixAxis() override; ~Controller_ConsoleSixAxis() override;
// Called when the controller is initialized // Called when the controller is initialized
@ -30,7 +34,7 @@ public:
void OnUpdate(const Core::Timing::CoreTiming& core_timing) override; void OnUpdate(const Core::Timing::CoreTiming& core_timing) override;
// Called on InitializeSevenSixAxisSensor // Called on InitializeSevenSixAxisSensor
void SetTransferMemoryPointer(u8* t_mem); void SetTransferMemoryAddress(VAddr t_mem);
// Called on ResetSevenSixAxisSensorTimestamp // Called on ResetSevenSixAxisSensorTimestamp
void ResetTimestamp(); void ResetTimestamp();
@ -62,12 +66,13 @@ private:
static_assert(sizeof(seven_sixaxis_lifo) == 0xA70, "SevenSixAxisState is an invalid size"); static_assert(sizeof(seven_sixaxis_lifo) == 0xA70, "SevenSixAxisState is an invalid size");
SevenSixAxisState next_seven_sixaxis_state{}; SevenSixAxisState next_seven_sixaxis_state{};
u8* transfer_memory = nullptr; VAddr transfer_memory{};
ConsoleSharedMemory* shared_memory = nullptr; ConsoleSharedMemory* shared_memory = nullptr;
Core::HID::EmulatedConsole* console = nullptr; Core::HID::EmulatedConsole* console = nullptr;
bool is_transfer_memory_set = false;
u64 last_saved_timestamp{}; u64 last_saved_timestamp{};
u64 last_global_timestamp{}; u64 last_global_timestamp{};
Core::System& system;
}; };
} // namespace Service::HID } // namespace Service::HID

@ -152,7 +152,7 @@ Result Controller_Palma::WritePalmaRgbLedPatternEntry(const PalmaConnectionHandl
} }
Result Controller_Palma::WritePalmaWaveEntry(const PalmaConnectionHandle& handle, PalmaWaveSet wave, Result Controller_Palma::WritePalmaWaveEntry(const PalmaConnectionHandle& handle, PalmaWaveSet wave,
u8* t_mem, u64 size) { VAddr t_mem, u64 size) {
if (handle.npad_id != active_handle.npad_id) { if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle; return InvalidPalmaHandle;
} }

@ -125,7 +125,7 @@ public:
Result ReadPalmaUniqueCode(const PalmaConnectionHandle& handle); Result ReadPalmaUniqueCode(const PalmaConnectionHandle& handle);
Result SetPalmaUniqueCodeInvalid(const PalmaConnectionHandle& handle); Result SetPalmaUniqueCodeInvalid(const PalmaConnectionHandle& handle);
Result WritePalmaRgbLedPatternEntry(const PalmaConnectionHandle& handle, u64 unknown); Result WritePalmaRgbLedPatternEntry(const PalmaConnectionHandle& handle, u64 unknown);
Result WritePalmaWaveEntry(const PalmaConnectionHandle& handle, PalmaWaveSet wave, u8* t_mem, Result WritePalmaWaveEntry(const PalmaConnectionHandle& handle, PalmaWaveSet wave, VAddr t_mem,
u64 size); u64 size);
Result SetPalmaDataBaseIdentificationVersion(const PalmaConnectionHandle& handle, Result SetPalmaDataBaseIdentificationVersion(const PalmaConnectionHandle& handle,
s32 database_id_version_); s32 database_id_version_);

@ -1858,7 +1858,7 @@ void Hid::InitializeSevenSixAxisSensor(Kernel::HLERequestContext& ctx) {
.ActivateController(); .ActivateController();
applet_resource->GetController<Controller_ConsoleSixAxis>(HidController::ConsoleSixAxisSensor) applet_resource->GetController<Controller_ConsoleSixAxis>(HidController::ConsoleSixAxisSensor)
.SetTransferMemoryPointer(system.Memory().GetPointer(t_mem_1->GetSourceAddress())); .SetTransferMemoryAddress(t_mem_1->GetSourceAddress());
LOG_WARNING(Service_HID, LOG_WARNING(Service_HID,
"called, t_mem_1_handle=0x{:08X}, t_mem_2_handle=0x{:08X}, " "called, t_mem_1_handle=0x{:08X}, t_mem_2_handle=0x{:08X}, "
@ -2145,8 +2145,7 @@ void Hid::WritePalmaWaveEntry(Kernel::HLERequestContext& ctx) {
connection_handle.npad_id, wave_set, unknown, t_mem_handle, t_mem_size, size); connection_handle.npad_id, wave_set, unknown, t_mem_handle, t_mem_size, size);
applet_resource->GetController<Controller_Palma>(HidController::Palma) applet_resource->GetController<Controller_Palma>(HidController::Palma)
.WritePalmaWaveEntry(connection_handle, wave_set, .WritePalmaWaveEntry(connection_handle, wave_set, t_mem->GetSourceAddress(), t_mem_size);
system.Memory().GetPointer(t_mem->GetSourceAddress()), t_mem_size);
IPC::ResponseBuilder rb{ctx, 2}; IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ResultSuccess); rb.Push(ResultSuccess);

@ -60,9 +60,15 @@ public:
private: private:
template <typename T> template <typename T>
void MakeController(HidController controller, u8* shared_memory) { void MakeController(HidController controller, u8* shared_memory) {
if constexpr (std::is_constructible_v<T, Core::System&, u8*>) {
controllers[static_cast<std::size_t>(controller)] =
std::make_unique<T>(system, shared_memory);
} else {
controllers[static_cast<std::size_t>(controller)] = controllers[static_cast<std::size_t>(controller)] =
std::make_unique<T>(system.HIDCore(), shared_memory); std::make_unique<T>(system.HIDCore(), shared_memory);
} }
}
template <typename T> template <typename T>
void MakeControllerWithServiceContext(HidController controller, u8* shared_memory) { void MakeControllerWithServiceContext(HidController controller, u8* shared_memory) {
controllers[static_cast<std::size_t>(controller)] = controllers[static_cast<std::size_t>(controller)] =

@ -472,7 +472,7 @@ void HidBus::EnableJoyPollingReceiveMode(Kernel::HLERequestContext& ctx) {
if (device_index) { if (device_index) {
auto& device = devices[device_index.value()].device; auto& device = devices[device_index.value()].device;
device->SetPollingMode(polling_mode_); device->SetPollingMode(polling_mode_);
device->SetTransferMemoryPointer(system.Memory().GetPointer(t_mem->GetSourceAddress())); device->SetTransferMemoryAddress(t_mem->GetSourceAddress());
IPC::ResponseBuilder rb{ctx, 2}; IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ResultSuccess); rb.Push(ResultSuccess);

@ -115,8 +115,7 @@ private:
void MakeDevice(BusHandle handle) { void MakeDevice(BusHandle handle) {
const auto device_index = GetDeviceIndexFromHandle(handle); const auto device_index = GetDeviceIndexFromHandle(handle);
if (device_index) { if (device_index) {
devices[device_index.value()].device = devices[device_index.value()].device = std::make_unique<T>(system, service_context);
std::make_unique<T>(system.HIDCore(), service_context);
} }
} }

@ -9,8 +9,8 @@
namespace Service::HID { namespace Service::HID {
HidbusBase::HidbusBase(KernelHelpers::ServiceContext& service_context_) HidbusBase::HidbusBase(Core::System& system_, KernelHelpers::ServiceContext& service_context_)
: service_context(service_context_) { : system(system_), service_context(service_context_) {
send_command_async_event = service_context.CreateEvent("hidbus:SendCommandAsyncEvent"); send_command_async_event = service_context.CreateEvent("hidbus:SendCommandAsyncEvent");
} }
HidbusBase::~HidbusBase() = default; HidbusBase::~HidbusBase() = default;
@ -59,8 +59,7 @@ void HidbusBase::DisablePollingMode() {
polling_mode_enabled = false; polling_mode_enabled = false;
} }
void HidbusBase::SetTransferMemoryPointer(u8* t_mem) { void HidbusBase::SetTransferMemoryAddress(VAddr t_mem) {
is_transfer_memory_set = true;
transfer_memory = t_mem; transfer_memory = t_mem;
} }

@ -8,6 +8,10 @@
#include "common/common_types.h" #include "common/common_types.h"
#include "core/hle/result.h" #include "core/hle/result.h"
namespace Core {
class System;
}
namespace Kernel { namespace Kernel {
class KEvent; class KEvent;
class KReadableEvent; class KReadableEvent;
@ -106,7 +110,7 @@ static_assert(sizeof(ButtonOnlyPollingDataAccessor) == 0x2F0,
class HidbusBase { class HidbusBase {
public: public:
explicit HidbusBase(KernelHelpers::ServiceContext& service_context_); explicit HidbusBase(Core::System& system_, KernelHelpers::ServiceContext& service_context_);
virtual ~HidbusBase(); virtual ~HidbusBase();
void ActivateDevice(); void ActivateDevice();
@ -134,7 +138,7 @@ public:
void DisablePollingMode(); void DisablePollingMode();
// Called on EnableJoyPollingReceiveMode // Called on EnableJoyPollingReceiveMode
void SetTransferMemoryPointer(u8* t_mem); void SetTransferMemoryAddress(VAddr t_mem);
Kernel::KReadableEvent& GetSendCommandAsycEvent() const; Kernel::KReadableEvent& GetSendCommandAsycEvent() const;
@ -170,9 +174,9 @@ protected:
JoyEnableSixAxisDataAccessor enable_sixaxis_data{}; JoyEnableSixAxisDataAccessor enable_sixaxis_data{};
ButtonOnlyPollingDataAccessor button_only_data{}; ButtonOnlyPollingDataAccessor button_only_data{};
u8* transfer_memory{nullptr}; VAddr transfer_memory{};
bool is_transfer_memory_set{};
Core::System& system;
Kernel::KEvent* send_command_async_event; Kernel::KEvent* send_command_async_event;
KernelHelpers::ServiceContext& service_context; KernelHelpers::ServiceContext& service_context;
}; };

@ -1,18 +1,20 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#include "core/core.h"
#include "core/hid/emulated_controller.h" #include "core/hid/emulated_controller.h"
#include "core/hid/hid_core.h" #include "core/hid/hid_core.h"
#include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_event.h"
#include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_readable_event.h"
#include "core/hle/service/hid/hidbus/ringcon.h" #include "core/hle/service/hid/hidbus/ringcon.h"
#include "core/memory.h"
namespace Service::HID { namespace Service::HID {
RingController::RingController(Core::HID::HIDCore& hid_core_, RingController::RingController(Core::System& system_,
KernelHelpers::ServiceContext& service_context_) KernelHelpers::ServiceContext& service_context_)
: HidbusBase(service_context_) { : HidbusBase(system_, service_context_) {
input = hid_core_.GetEmulatedController(Core::HID::NpadIdType::Player1); input = system.HIDCore().GetEmulatedController(Core::HID::NpadIdType::Player1);
} }
RingController::~RingController() = default; RingController::~RingController() = default;
@ -38,7 +40,7 @@ void RingController::OnUpdate() {
return; return;
} }
if (!polling_mode_enabled || !is_transfer_memory_set) { if (!polling_mode_enabled || transfer_memory == 0) {
return; return;
} }
@ -62,7 +64,8 @@ void RingController::OnUpdate() {
curr_entry.polling_data.out_size = sizeof(ringcon_value); curr_entry.polling_data.out_size = sizeof(ringcon_value);
std::memcpy(curr_entry.polling_data.data.data(), &ringcon_value, sizeof(ringcon_value)); std::memcpy(curr_entry.polling_data.data.data(), &ringcon_value, sizeof(ringcon_value));
std::memcpy(transfer_memory, &enable_sixaxis_data, sizeof(enable_sixaxis_data)); system.Memory().WriteBlock(transfer_memory, &enable_sixaxis_data,
sizeof(enable_sixaxis_data));
break; break;
} }
default: default:

@ -17,8 +17,7 @@ namespace Service::HID {
class RingController final : public HidbusBase { class RingController final : public HidbusBase {
public: public:
explicit RingController(Core::HID::HIDCore& hid_core_, explicit RingController(Core::System& system_, KernelHelpers::ServiceContext& service_context_);
KernelHelpers::ServiceContext& service_context_);
~RingController() override; ~RingController() override;
void OnInit() override; void OnInit() override;

@ -8,8 +8,8 @@
namespace Service::HID { namespace Service::HID {
constexpr u8 DEVICE_ID = 0x28; constexpr u8 DEVICE_ID = 0x28;
Starlink::Starlink(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_) Starlink::Starlink(Core::System& system_, KernelHelpers::ServiceContext& service_context_)
: HidbusBase(service_context_) {} : HidbusBase(system_, service_context_) {}
Starlink::~Starlink() = default; Starlink::~Starlink() = default;
void Starlink::OnInit() { void Starlink::OnInit() {
@ -27,7 +27,7 @@ void Starlink::OnUpdate() {
if (!device_enabled) { if (!device_enabled) {
return; return;
} }
if (!polling_mode_enabled || !is_transfer_memory_set) { if (!polling_mode_enabled || transfer_memory == 0) {
return; return;
} }

@ -14,8 +14,7 @@ namespace Service::HID {
class Starlink final : public HidbusBase { class Starlink final : public HidbusBase {
public: public:
explicit Starlink(Core::HID::HIDCore& hid_core_, explicit Starlink(Core::System& system_, KernelHelpers::ServiceContext& service_context_);
KernelHelpers::ServiceContext& service_context_);
~Starlink() override; ~Starlink() override;
void OnInit() override; void OnInit() override;

@ -8,9 +8,8 @@
namespace Service::HID { namespace Service::HID {
constexpr u8 DEVICE_ID = 0xFF; constexpr u8 DEVICE_ID = 0xFF;
HidbusStubbed::HidbusStubbed(Core::HID::HIDCore& hid_core_, HidbusStubbed::HidbusStubbed(Core::System& system_, KernelHelpers::ServiceContext& service_context_)
KernelHelpers::ServiceContext& service_context_) : HidbusBase(system_, service_context_) {}
: HidbusBase(service_context_) {}
HidbusStubbed::~HidbusStubbed() = default; HidbusStubbed::~HidbusStubbed() = default;
void HidbusStubbed::OnInit() { void HidbusStubbed::OnInit() {
@ -28,7 +27,7 @@ void HidbusStubbed::OnUpdate() {
if (!device_enabled) { if (!device_enabled) {
return; return;
} }
if (!polling_mode_enabled || !is_transfer_memory_set) { if (!polling_mode_enabled || transfer_memory == 0) {
return; return;
} }

@ -14,8 +14,7 @@ namespace Service::HID {
class HidbusStubbed final : public HidbusBase { class HidbusStubbed final : public HidbusBase {
public: public:
explicit HidbusStubbed(Core::HID::HIDCore& hid_core_, explicit HidbusStubbed(Core::System& system_, KernelHelpers::ServiceContext& service_context_);
KernelHelpers::ServiceContext& service_context_);
~HidbusStubbed() override; ~HidbusStubbed() override;
void OnInit() override; void OnInit() override;

@ -208,8 +208,6 @@ void IRS::RunImageTransferProcessor(Kernel::HLERequestContext& ctx) {
ASSERT_MSG(t_mem->GetSize() == parameters.transfer_memory_size, "t_mem has incorrect size"); ASSERT_MSG(t_mem->GetSize() == parameters.transfer_memory_size, "t_mem has incorrect size");
u8* transfer_memory = system.Memory().GetPointer(t_mem->GetSourceAddress());
LOG_INFO(Service_IRS, LOG_INFO(Service_IRS,
"called, npad_type={}, npad_id={}, transfer_memory_size={}, transfer_memory_size={}, " "called, npad_type={}, npad_id={}, transfer_memory_size={}, transfer_memory_size={}, "
"applet_resource_user_id={}", "applet_resource_user_id={}",
@ -224,7 +222,7 @@ void IRS::RunImageTransferProcessor(Kernel::HLERequestContext& ctx) {
auto& image_transfer_processor = auto& image_transfer_processor =
GetProcessor<ImageTransferProcessor>(parameters.camera_handle); GetProcessor<ImageTransferProcessor>(parameters.camera_handle);
image_transfer_processor.SetConfig(parameters.processor_config); image_transfer_processor.SetConfig(parameters.processor_config);
image_transfer_processor.SetTransferMemoryPointer(transfer_memory); image_transfer_processor.SetTransferMemoryAddress(t_mem->GetSourceAddress());
npad_device->SetPollingMode(Core::HID::EmulatedDeviceIndex::RightIndex, npad_device->SetPollingMode(Core::HID::EmulatedDeviceIndex::RightIndex,
Common::Input::PollingMode::IR); Common::Input::PollingMode::IR);
} }
@ -448,8 +446,6 @@ void IRS::RunImageTransferExProcessor(Kernel::HLERequestContext& ctx) {
auto t_mem = system.ApplicationProcess()->GetHandleTable().GetObject<Kernel::KTransferMemory>( auto t_mem = system.ApplicationProcess()->GetHandleTable().GetObject<Kernel::KTransferMemory>(
t_mem_handle); t_mem_handle);
u8* transfer_memory = system.Memory().GetPointer(t_mem->GetSourceAddress());
LOG_INFO(Service_IRS, LOG_INFO(Service_IRS,
"called, npad_type={}, npad_id={}, transfer_memory_size={}, " "called, npad_type={}, npad_id={}, transfer_memory_size={}, "
"applet_resource_user_id={}", "applet_resource_user_id={}",
@ -464,7 +460,7 @@ void IRS::RunImageTransferExProcessor(Kernel::HLERequestContext& ctx) {
auto& image_transfer_processor = auto& image_transfer_processor =
GetProcessor<ImageTransferProcessor>(parameters.camera_handle); GetProcessor<ImageTransferProcessor>(parameters.camera_handle);
image_transfer_processor.SetConfig(parameters.processor_config); image_transfer_processor.SetConfig(parameters.processor_config);
image_transfer_processor.SetTransferMemoryPointer(transfer_memory); image_transfer_processor.SetTransferMemoryAddress(t_mem->GetSourceAddress());
npad_device->SetPollingMode(Core::HID::EmulatedDeviceIndex::RightIndex, npad_device->SetPollingMode(Core::HID::EmulatedDeviceIndex::RightIndex,
Common::Input::PollingMode::IR); Common::Input::PollingMode::IR);
} }

@ -80,8 +80,14 @@ private:
LOG_CRITICAL(Service_IRS, "Invalid index {}", index); LOG_CRITICAL(Service_IRS, "Invalid index {}", index);
return; return;
} }
if constexpr (std::is_constructible_v<T, Core::System&, Core::IrSensor::DeviceFormat&,
std::size_t>) {
processors[index] = std::make_unique<T>(system, device_state, index);
} else {
processors[index] = std::make_unique<T>(system.HIDCore(), device_state, index); processors[index] = std::make_unique<T>(system.HIDCore(), device_state, index);
} }
}
template <typename T> template <typename T>
T& GetProcessor(const Core::IrSensor::IrCameraHandle& handle) { T& GetProcessor(const Core::IrSensor::IrCameraHandle& handle) {

@ -1,16 +1,18 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
#include "core/core.h"
#include "core/hid/emulated_controller.h" #include "core/hid/emulated_controller.h"
#include "core/hid/hid_core.h" #include "core/hid/hid_core.h"
#include "core/hle/service/hid/irsensor/image_transfer_processor.h" #include "core/hle/service/hid/irsensor/image_transfer_processor.h"
#include "core/memory.h"
namespace Service::IRS { namespace Service::IRS {
ImageTransferProcessor::ImageTransferProcessor(Core::HID::HIDCore& hid_core_, ImageTransferProcessor::ImageTransferProcessor(Core::System& system_,
Core::IrSensor::DeviceFormat& device_format, Core::IrSensor::DeviceFormat& device_format,
std::size_t npad_index) std::size_t npad_index)
: device{device_format} { : device{device_format}, system{system_} {
npad_device = hid_core_.GetEmulatedControllerByIndex(npad_index); npad_device = system.HIDCore().GetEmulatedControllerByIndex(npad_index);
Core::HID::ControllerUpdateCallback engine_callback{ Core::HID::ControllerUpdateCallback engine_callback{
.on_change = [this](Core::HID::ControllerTriggerType type) { OnControllerUpdate(type); }, .on_change = [this](Core::HID::ControllerTriggerType type) { OnControllerUpdate(type); },
@ -43,7 +45,7 @@ void ImageTransferProcessor::OnControllerUpdate(Core::HID::ControllerTriggerType
if (type != Core::HID::ControllerTriggerType::IrSensor) { if (type != Core::HID::ControllerTriggerType::IrSensor) {
return; return;
} }
if (!is_transfer_memory_set) { if (transfer_memory == 0) {
return; return;
} }
@ -56,14 +58,16 @@ void ImageTransferProcessor::OnControllerUpdate(Core::HID::ControllerTriggerType
if (camera_data.format != current_config.origin_format) { if (camera_data.format != current_config.origin_format) {
LOG_WARNING(Service_IRS, "Wrong Input format {} expected {}", camera_data.format, LOG_WARNING(Service_IRS, "Wrong Input format {} expected {}", camera_data.format,
current_config.origin_format); current_config.origin_format);
memset(transfer_memory, 0, GetDataSize(current_config.trimming_format)); system.Memory().ZeroBlock(*system.ApplicationProcess(), transfer_memory,
GetDataSize(current_config.trimming_format));
return; return;
} }
if (current_config.origin_format > current_config.trimming_format) { if (current_config.origin_format > current_config.trimming_format) {
LOG_WARNING(Service_IRS, "Origin format {} is smaller than trimming format {}", LOG_WARNING(Service_IRS, "Origin format {} is smaller than trimming format {}",
current_config.origin_format, current_config.trimming_format); current_config.origin_format, current_config.trimming_format);
memset(transfer_memory, 0, GetDataSize(current_config.trimming_format)); system.Memory().ZeroBlock(*system.ApplicationProcess(), transfer_memory,
GetDataSize(current_config.trimming_format));
return; return;
} }
@ -80,7 +84,8 @@ void ImageTransferProcessor::OnControllerUpdate(Core::HID::ControllerTriggerType
"Trimming area ({}, {}, {}, {}) is outside of origin area ({}, {})", "Trimming area ({}, {}, {}, {}) is outside of origin area ({}, {})",
current_config.trimming_start_x, current_config.trimming_start_y, current_config.trimming_start_x, current_config.trimming_start_y,
trimming_width, trimming_height, origin_width, origin_height); trimming_width, trimming_height, origin_width, origin_height);
memset(transfer_memory, 0, GetDataSize(current_config.trimming_format)); system.Memory().ZeroBlock(*system.ApplicationProcess(), transfer_memory,
GetDataSize(current_config.trimming_format));
return; return;
} }
@ -94,7 +99,8 @@ void ImageTransferProcessor::OnControllerUpdate(Core::HID::ControllerTriggerType
} }
} }
memcpy(transfer_memory, window_data.data(), GetDataSize(current_config.trimming_format)); system.Memory().WriteBlock(transfer_memory, window_data.data(),
GetDataSize(current_config.trimming_format));
if (!IsProcessorActive()) { if (!IsProcessorActive()) {
StartProcessor(); StartProcessor();
@ -134,8 +140,7 @@ void ImageTransferProcessor::SetConfig(
npad_device->SetCameraFormat(current_config.origin_format); npad_device->SetCameraFormat(current_config.origin_format);
} }
void ImageTransferProcessor::SetTransferMemoryPointer(u8* t_mem) { void ImageTransferProcessor::SetTransferMemoryAddress(VAddr t_mem) {
is_transfer_memory_set = true;
transfer_memory = t_mem; transfer_memory = t_mem;
} }
@ -143,7 +148,7 @@ Core::IrSensor::ImageTransferProcessorState ImageTransferProcessor::GetState(
std::vector<u8>& data) const { std::vector<u8>& data) const {
const auto size = GetDataSize(current_config.trimming_format); const auto size = GetDataSize(current_config.trimming_format);
data.resize(size); data.resize(size);
memcpy(data.data(), transfer_memory, size); system.Memory().ReadBlock(transfer_memory, data.data(), size);
return processor_state; return processor_state;
} }

@ -7,6 +7,10 @@
#include "core/hid/irs_types.h" #include "core/hid/irs_types.h"
#include "core/hle/service/hid/irsensor/processor_base.h" #include "core/hle/service/hid/irsensor/processor_base.h"
namespace Core {
class System;
}
namespace Core::HID { namespace Core::HID {
class EmulatedController; class EmulatedController;
} // namespace Core::HID } // namespace Core::HID
@ -14,7 +18,7 @@ class EmulatedController;
namespace Service::IRS { namespace Service::IRS {
class ImageTransferProcessor final : public ProcessorBase { class ImageTransferProcessor final : public ProcessorBase {
public: public:
explicit ImageTransferProcessor(Core::HID::HIDCore& hid_core_, explicit ImageTransferProcessor(Core::System& system_,
Core::IrSensor::DeviceFormat& device_format, Core::IrSensor::DeviceFormat& device_format,
std::size_t npad_index); std::size_t npad_index);
~ImageTransferProcessor() override; ~ImageTransferProcessor() override;
@ -33,7 +37,7 @@ public:
void SetConfig(Core::IrSensor::PackedImageTransferProcessorExConfig config); void SetConfig(Core::IrSensor::PackedImageTransferProcessorExConfig config);
// Transfer memory where the image data will be stored // Transfer memory where the image data will be stored
void SetTransferMemoryPointer(u8* t_mem); void SetTransferMemoryAddress(VAddr t_mem);
Core::IrSensor::ImageTransferProcessorState GetState(std::vector<u8>& data) const; Core::IrSensor::ImageTransferProcessorState GetState(std::vector<u8>& data) const;
@ -67,7 +71,7 @@ private:
Core::HID::EmulatedController* npad_device; Core::HID::EmulatedController* npad_device;
int callback_key{}; int callback_key{};
u8* transfer_memory = nullptr; Core::System& system;
bool is_transfer_memory_set = false; VAddr transfer_memory{};
}; };
} // namespace Service::IRS } // namespace Service::IRS