2020-10-30 19:54:30 +01:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-10-30 19:54:30 +01:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
#include "gpio_remote_manager_impl.hpp"
|
|
|
|
|
|
|
|
namespace ams::gpio {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constinit os::SdkMutex g_init_mutex;
|
|
|
|
constinit int g_initialize_count = 0;
|
2021-01-17 16:55:32 +01:00
|
|
|
ams::sf::SharedPointer<gpio::sf::IManager> g_manager;
|
2020-10-30 19:54:30 +01:00
|
|
|
|
2022-03-06 21:08:20 +01:00
|
|
|
#if defined(ATMOSPHERE_OS_HORIZON)
|
|
|
|
constinit bool g_remote = false;
|
2021-01-17 16:55:32 +01:00
|
|
|
ams::sf::UnmanagedServiceObject<gpio::sf::IManager, RemoteManagerImpl> g_remote_manager_impl;
|
2022-03-06 21:08:20 +01:00
|
|
|
#endif
|
2020-10-30 19:54:30 +01:00
|
|
|
|
2021-01-17 16:55:32 +01:00
|
|
|
gpio::sf::IPadSession *GetInterface(GpioPadSession *session) {
|
2020-10-30 19:54:30 +01:00
|
|
|
AMS_ASSERT(session->_session != nullptr);
|
2021-01-17 16:55:32 +01:00
|
|
|
return static_cast<gpio::sf::IPadSession *>(session->_session);
|
2020-10-30 19:54:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Initialize() {
|
|
|
|
std::scoped_lock lk(g_init_mutex);
|
|
|
|
|
|
|
|
if ((g_initialize_count++) == 0) {
|
2022-03-06 21:08:20 +01:00
|
|
|
#if defined(ATMOSPHERE_OS_HORIZON)
|
2020-10-30 19:54:30 +01:00
|
|
|
R_ABORT_UNLESS(::gpioInitialize());
|
2021-01-17 16:55:32 +01:00
|
|
|
g_manager = g_remote_manager_impl.GetShared();
|
2020-10-31 11:22:01 +01:00
|
|
|
g_remote = true;
|
2022-03-06 21:08:20 +01:00
|
|
|
#else
|
|
|
|
AMS_ABORT("TODO");
|
|
|
|
#endif
|
2020-10-30 19:54:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 16:55:32 +01:00
|
|
|
void InitializeWith(ams::sf::SharedPointer<gpio::sf::IManager> manager) {
|
2020-10-31 11:22:01 +01:00
|
|
|
std::scoped_lock lk(g_init_mutex);
|
|
|
|
|
|
|
|
AMS_ABORT_UNLESS(g_initialize_count == 0);
|
|
|
|
|
2021-01-17 16:55:32 +01:00
|
|
|
g_manager = manager;
|
2020-10-31 11:22:01 +01:00
|
|
|
g_initialize_count = 1;
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:54:30 +01:00
|
|
|
void Finalize() {
|
|
|
|
std::scoped_lock lk(g_init_mutex);
|
|
|
|
|
|
|
|
AMS_ASSERT(g_initialize_count > 0);
|
|
|
|
|
|
|
|
if ((--g_initialize_count) == 0) {
|
2021-01-17 16:55:32 +01:00
|
|
|
g_manager = nullptr;
|
2022-03-06 21:08:20 +01:00
|
|
|
#if defined(ATMOSPHERE_OS_HORIZON)
|
2020-10-31 11:22:01 +01:00
|
|
|
if (g_remote) {
|
|
|
|
::gpioExit();
|
|
|
|
}
|
2022-03-06 21:08:20 +01:00
|
|
|
#else
|
|
|
|
AMS_ABORT("TODO");
|
|
|
|
#endif
|
2020-10-30 19:54:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Result OpenSession(GpioPadSession *out_session, ams::DeviceCode device_code) {
|
|
|
|
/* Get the session. */
|
2021-01-17 16:55:32 +01:00
|
|
|
ams::sf::SharedPointer<gpio::sf::IPadSession> session;
|
2020-10-30 19:54:30 +01:00
|
|
|
{
|
|
|
|
if (hos::GetVersion() >= hos::Version_7_0_0) {
|
2021-01-17 16:55:32 +01:00
|
|
|
R_TRY(g_manager->OpenSession2(std::addressof(session), device_code, ddsf::AccessMode_ReadWrite));
|
2020-10-30 19:54:30 +01:00
|
|
|
} else {
|
2021-01-17 16:55:32 +01:00
|
|
|
R_TRY(g_manager->OpenSession(std::addressof(session), ConvertToGpioPadName(device_code)));
|
2020-10-30 19:54:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set output. */
|
2021-01-17 16:55:32 +01:00
|
|
|
out_session->_session = session.Detach();
|
2020-10-30 19:54:30 +01:00
|
|
|
out_session->_event = nullptr;
|
|
|
|
|
|
|
|
/* We succeeded. */
|
2022-03-26 08:14:36 +01:00
|
|
|
R_SUCCEED();
|
2020-10-30 19:54:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloseSession(GpioPadSession *session) {
|
|
|
|
AMS_ASSERT(session != nullptr);
|
|
|
|
|
|
|
|
/* Unbind the interrupt, if it's still bound. */
|
|
|
|
if (session->_event != nullptr) {
|
|
|
|
gpio::UnbindInterrupt(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the session. */
|
2021-01-17 16:55:32 +01:00
|
|
|
ams::sf::ReleaseSharedObject(GetInterface(session));
|
2020-10-30 19:54:30 +01:00
|
|
|
session->_session = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result IsWakeEventActive(bool *out_is_active, ams::DeviceCode device_code) {
|
|
|
|
if (hos::GetVersion() >= hos::Version_7_0_0) {
|
|
|
|
R_TRY(g_manager->IsWakeEventActive2(out_is_active, device_code));
|
|
|
|
} else {
|
|
|
|
R_TRY(g_manager->IsWakeEventActive(out_is_active, ConvertToGpioPadName(device_code)));
|
|
|
|
}
|
|
|
|
|
2022-03-26 08:14:36 +01:00
|
|
|
R_SUCCEED();
|
2020-10-30 19:54:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Direction GetDirection(GpioPadSession *session) {
|
|
|
|
Direction out;
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->GetDirection(std::addressof(out)));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetDirection(GpioPadSession *session, Direction direction) {
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->SetDirection(direction));
|
|
|
|
}
|
|
|
|
|
|
|
|
GpioValue GetValue(GpioPadSession *session) {
|
|
|
|
GpioValue out;
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->GetValue(std::addressof(out)));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetValue(GpioPadSession *session, GpioValue value) {
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->SetValue(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
InterruptMode GetInterruptMode(GpioPadSession *session) {
|
|
|
|
InterruptMode out;
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->GetInterruptMode(std::addressof(out)));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetInterruptMode(GpioPadSession *session, InterruptMode mode) {
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->SetInterruptMode(mode));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetInterruptEnable(GpioPadSession *session) {
|
|
|
|
bool out;
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->GetInterruptEnable(std::addressof(out)));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetInterruptEnable(GpioPadSession *session, bool en) {
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->SetInterruptEnable(en));
|
|
|
|
}
|
|
|
|
|
|
|
|
InterruptStatus GetInterruptStatus(GpioPadSession *session) {
|
|
|
|
InterruptStatus out;
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->GetInterruptStatus(std::addressof(out)));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearInterruptStatus(GpioPadSession *session) {
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->ClearInterruptStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetDebounceTime(GpioPadSession *session) {
|
|
|
|
int out;
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->GetDebounceTime(std::addressof(out)));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetDebounceTime(GpioPadSession *session, int ms) {
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->SetDebounceTime(ms));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetDebounceEnabled(GpioPadSession *session) {
|
|
|
|
bool out;
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->GetDebounceEnabled(std::addressof(out)));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetDebounceEnabled(GpioPadSession *session, bool en) {
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->SetDebounceEnabled(en));
|
|
|
|
}
|
|
|
|
|
|
|
|
Result BindInterrupt(os::SystemEventType *event, GpioPadSession *session) {
|
|
|
|
AMS_ASSERT(session->_event == nullptr);
|
|
|
|
|
2021-10-05 09:11:36 +02:00
|
|
|
ams::sf::NativeHandle handle;
|
2020-10-30 19:54:30 +01:00
|
|
|
R_TRY(GetInterface(session)->BindInterrupt(std::addressof(handle)));
|
|
|
|
|
2021-10-05 09:11:36 +02:00
|
|
|
os::AttachReadableHandleToSystemEvent(event, handle.GetOsHandle(), handle.IsManaged(), os::EventClearMode_ManualClear);
|
|
|
|
handle.Detach();
|
2020-10-30 19:54:30 +01:00
|
|
|
|
|
|
|
session->_event = event;
|
2022-03-26 08:14:36 +01:00
|
|
|
R_SUCCEED();
|
2020-10-30 19:54:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnbindInterrupt(GpioPadSession *session) {
|
|
|
|
AMS_ASSERT(session->_event != nullptr);
|
|
|
|
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->UnbindInterrupt());
|
|
|
|
|
|
|
|
os::DestroySystemEvent(session->_event);
|
|
|
|
session->_event = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|