From 83c1c175ba594fccf7f70e56e7507b3840a2bf61 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sun, 7 Feb 2021 18:34:12 -0800 Subject: [PATCH] htc: skeleton some more of main --- .../libstratosphere/include/stratosphere.hpp | 4 + .../include/stratosphere/htc.hpp | 17 ++++ .../include/stratosphere/htcfs.hpp | 17 ++++ .../include/stratosphere/htclow.hpp | 18 ++++ .../htclow/htclow_manager_holder.hpp | 35 ++++++++ .../stratosphere/htclow/htclow_types.hpp | 33 +++++++ .../include/stratosphere/htcs.hpp | 17 ++++ .../source/htclow/htclow_manager_holder.cpp | 85 +++++++++++++++++++ stratosphere/htc/source/htc_main.cpp | 42 +++++++++ 9 files changed, 268 insertions(+) create mode 100644 libraries/libstratosphere/include/stratosphere/htc.hpp create mode 100644 libraries/libstratosphere/include/stratosphere/htcfs.hpp create mode 100644 libraries/libstratosphere/include/stratosphere/htclow.hpp create mode 100644 libraries/libstratosphere/include/stratosphere/htclow/htclow_manager_holder.hpp create mode 100644 libraries/libstratosphere/include/stratosphere/htclow/htclow_types.hpp create mode 100644 libraries/libstratosphere/include/stratosphere/htcs.hpp create mode 100644 libraries/libstratosphere/source/htclow/htclow_manager_holder.cpp diff --git a/libraries/libstratosphere/include/stratosphere.hpp b/libraries/libstratosphere/include/stratosphere.hpp index f9c66eb31..2b083f991 100644 --- a/libraries/libstratosphere/include/stratosphere.hpp +++ b/libraries/libstratosphere/include/stratosphere.hpp @@ -53,6 +53,10 @@ #include #include #include +#include +#include +#include +#include #include #include #include diff --git a/libraries/libstratosphere/include/stratosphere/htc.hpp b/libraries/libstratosphere/include/stratosphere/htc.hpp new file mode 100644 index 000000000..999d1a35e --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/htc.hpp @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + diff --git a/libraries/libstratosphere/include/stratosphere/htcfs.hpp b/libraries/libstratosphere/include/stratosphere/htcfs.hpp new file mode 100644 index 000000000..999d1a35e --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/htcfs.hpp @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + diff --git a/libraries/libstratosphere/include/stratosphere/htclow.hpp b/libraries/libstratosphere/include/stratosphere/htclow.hpp new file mode 100644 index 000000000..4d21e65f7 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/htclow.hpp @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include +#include diff --git a/libraries/libstratosphere/include/stratosphere/htclow/htclow_manager_holder.hpp b/libraries/libstratosphere/include/stratosphere/htclow/htclow_manager_holder.hpp new file mode 100644 index 000000000..54ac1f924 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/htclow/htclow_manager_holder.hpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include +#include + +namespace ams::htclow { + + class HtclowManager; + + namespace HtclowManagerHolder { + + void AddReference(); + void Release(); + + HtclowManager *GetHtclowManager(); + + void SetDefaultDriver(htclow::impl::DriverType driver_type); + + } + +} diff --git a/libraries/libstratosphere/include/stratosphere/htclow/htclow_types.hpp b/libraries/libstratosphere/include/stratosphere/htclow/htclow_types.hpp new file mode 100644 index 000000000..b05857437 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/htclow/htclow_types.hpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include + +namespace ams::htclow { + + namespace impl { + + enum class DriverType { + Socket = 2, + Usb = 3, + HostBridge = 4, + PlainChannel = 5, + }; + + } + + +} diff --git a/libraries/libstratosphere/include/stratosphere/htcs.hpp b/libraries/libstratosphere/include/stratosphere/htcs.hpp new file mode 100644 index 000000000..999d1a35e --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/htcs.hpp @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + diff --git a/libraries/libstratosphere/source/htclow/htclow_manager_holder.cpp b/libraries/libstratosphere/source/htclow/htclow_manager_holder.cpp new file mode 100644 index 000000000..07ce8894c --- /dev/null +++ b/libraries/libstratosphere/source/htclow/htclow_manager_holder.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2019-2020 Adubbz, Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + +namespace ams::htclow::HtclowManagerHolder { + + namespace { + + constinit os::SdkMutex g_holder_mutex; + constinit int g_holder_reference_count = 0; + + mem::StandardAllocator g_allocator; + + constinit HtclowManager *g_manager = nullptr; + + constinit htclow::impl::DriverType g_default_driver_type = htclow::impl::DriverType::Socket; + + alignas(os::MemoryPageSize) u8 g_heap_buffer[928_KB]; + + } + + void AddReference() { + std::scoped_lock lk(g_holder_mutex); + + if ((g_holder_reference_count++) == 0) { + /* Initialize the allocator for the manager. */ + g_allocator.Initialize(g_heap_buffer, sizeof(g_heap_buffer)); + + /* TODO: Allocate the manager. */ + /* g_manager = g_allocator.Allocate(sizeof(HtclowManager), alignof(HtclowManager)); */ + + /* TODO: Construct the manager. */ + /* std::construct_at(g_manager, std::addressof(g_allocator)); */ + + /* TODO: Open the driver. */ + /* R_ABORT_UNLESS(g_manager->OpenDriver(g_default_driver_type)); */ + } + + AMS_ASSERT(g_holder_reference_count > 0); + } + + void Release() { + std::scoped_lock lk(g_holder_mutex); + + AMS_ASSERT(g_holder_reference_count > 0); + + if ((--g_holder_reference_count) == 0) { + /* TODO: Disconnect. */ + /* g_manager->Disconnect(); */ + + /* TODO: Close the driver. */ + /* g_manager->CloseDriver(); */ + + /* TODO: Destroy the manager. */ + /* std::destroy_at(g_manager); */ + /* g_allocator.Free(g_manager); */ + /* g_manager = nullptr; */ + + /* Finalize the allocator. */ + g_allocator.Finalize(); + } + } + + HtclowManager *GetHtclowManager() { + return g_manager; + } + + void SetDefaultDriver(htclow::impl::DriverType driver_type) { + g_default_driver_type = driver_type; + } + +} diff --git a/stratosphere/htc/source/htc_main.cpp b/stratosphere/htc/source/htc_main.cpp index c0d83289b..775dff411 100644 --- a/stratosphere/htc/source/htc_main.cpp +++ b/stratosphere/htc/source/htc_main.cpp @@ -132,12 +132,54 @@ void __libnx_free(void *mem) { AMS_ABORT("__libnx_free was called"); } +namespace ams::htc { + + namespace { + + constexpr htclow::impl::DriverType DefaultHtclowDriverType = htclow::impl::DriverType::Usb; + + htclow::impl::DriverType GetHtclowDriverType() { + /* Get the transport type. */ + char transport[0x10]; + if (settings::fwdbg::GetSettingsItemValue(transport, sizeof(transport), "bsp0", "tm_transport") == 0) { + return DefaultHtclowDriverType; + } + + /* Make the transport type case insensitive. */ + transport[util::size(transport) - 1] = '\x00'; + for (size_t i = 0; i < util::size(transport); ++i) { + transport[i] = std::tolower(static_cast(transport[i])); + } + + /* Select the transport. */ + if (std::strstr(transport, "usb")) { + return htclow::impl::DriverType::Usb; + } else if (std::strstr(transport, "hb")) { + return htclow::impl::DriverType::HostBridge; + } else if (std::strstr(transport, "plainchannel")) { + return htclow::impl::DriverType::PlainChannel; + } else { + return DefaultHtclowDriverType; + } + } + + } + +} + int main(int argc, char **argv) { /* Set thread name. */ os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(htc, Main)); AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(htc, Main)); + /* Get and set the default driver type. */ + const auto driver_type = htc::GetHtclowDriverType(); + htclow::HtclowManagerHolder::SetDefaultDriver(driver_type); + + /* Initialize the htclow manager. */ + htclow::HtclowManagerHolder::AddReference(); + /* TODO */ /* Cleanup */