Atmosphere/libraries/libvapours/source/sdmmc/impl/sdmmc_device_detector.cpp

273 lines
13 KiB
C++
Raw Normal View History

/*
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
*/
#if defined(ATMOSPHERE_IS_STRATOSPHERE)
#include <stratosphere.hpp>
#elif defined(ATMOSPHERE_IS_MESOSPHERE)
#include <mesosphere.hpp>
#elif defined(ATMOSPHERE_IS_EXOSPHERE)
#include <exosphere.hpp>
#else
#include <vapours.hpp>
#endif
#if defined(AMS_SDMMC_USE_SD_CARD_DETECTOR)
#include "sdmmc_device_detector.hpp"
namespace ams::sdmmc::impl {
bool DeviceDetector::IsCurrentInserted() {
2021-10-09 22:59:59 +00:00
return gpio::GetValue(std::addressof(m_gpio_pad_session)) == m_inserted_gpio_value;
}
void DeviceDetector::HandleDeviceStatus(bool prev_inserted, bool cur_inserted) {
if (!prev_inserted && !cur_inserted) {
/* Not inserted -> Not inserted, nothing to do. */
} else if (!prev_inserted && cur_inserted) {
/* Card was inserted. */
2021-10-09 22:59:59 +00:00
if (m_callback_info.inserted_callback != nullptr) {
m_callback_info.inserted_callback(m_callback_info.inserted_callback_arg);
}
} else if (prev_inserted && !cur_inserted) {
/* Card was removed. */
2021-10-09 22:59:59 +00:00
if (m_callback_info.removed_callback != nullptr) {
m_callback_info.removed_callback(m_callback_info.removed_callback_arg);
}
} else /* if (prev_inserted && cur_inserted) */ {
/* Card was removed, and then inserted. */
2021-10-09 22:59:59 +00:00
if (m_callback_info.removed_callback != nullptr) {
m_callback_info.removed_callback(m_callback_info.removed_callback_arg);
}
2021-10-09 22:59:59 +00:00
if (m_callback_info.inserted_callback != nullptr) {
m_callback_info.inserted_callback(m_callback_info.inserted_callback_arg);
}
}
}
void DeviceDetector::DetectorThread() {
/* Initialize the gpio session. */
gpio::Initialize();
/* Open and configure the pad session. */
2021-10-09 22:59:59 +00:00
gpio::OpenSession(std::addressof(m_gpio_pad_session), m_gpio_device_code);
gpio::SetDirection(std::addressof(m_gpio_pad_session), gpio::Direction_Input);
gpio::SetDebounceTime(std::addressof(m_gpio_pad_session), m_gpio_debounce_ms);
gpio::SetDebounceEnabled(std::addressof(m_gpio_pad_session), true);
gpio::SetInterruptMode(std::addressof(m_gpio_pad_session), gpio::InterruptMode_AnyEdge);
/* Get the gpio session's interrupt event. */
os::SystemEventType gpio_event;
2021-10-09 22:59:59 +00:00
R_ABORT_UNLESS(gpio::BindInterrupt(std::addressof(gpio_event), std::addressof(m_gpio_pad_session)));
/* Initialize and link multi wait/holders. */
os::MultiWaitType multi_wait;
os::MultiWaitHolderType detector_thread_end_holder;
os::MultiWaitHolderType request_sleep_wake_event_holder;
os::MultiWaitHolderType gpio_event_holder;
os::InitializeMultiWait(std::addressof(multi_wait));
2021-10-09 22:59:59 +00:00
os::InitializeMultiWaitHolder(std::addressof(detector_thread_end_holder), std::addressof(m_detector_thread_end_event));
os::LinkMultiWaitHolder(std::addressof(multi_wait), std::addressof(detector_thread_end_holder));
2021-10-09 22:59:59 +00:00
os::InitializeMultiWaitHolder(std::addressof(request_sleep_wake_event_holder), std::addressof(m_request_sleep_wake_event));
os::LinkMultiWaitHolder(std::addressof(multi_wait), std::addressof(request_sleep_wake_event_holder));
os::InitializeMultiWaitHolder(std::addressof(gpio_event_holder), std::addressof(gpio_event));
os::LinkMultiWaitHolder(std::addressof(multi_wait), std::addressof(gpio_event_holder));
/* Wait before detecting the initial state of the card. */
2021-10-09 22:59:59 +00:00
os::SleepThread(TimeSpan::FromMilliSeconds(m_gpio_debounce_ms));
bool cur_inserted = this->IsCurrentInserted();
2021-10-09 22:59:59 +00:00
m_is_prev_inserted = cur_inserted;
/* Set state as awake. */
2021-10-09 22:59:59 +00:00
m_state = State_Awake;
os::SignalEvent(std::addressof(m_ready_device_status_event));
/* Enable interrupts to be informed of device status. */
2021-10-09 22:59:59 +00:00
gpio::SetInterruptEnable(std::addressof(m_gpio_pad_session), true);
/* Wait, servicing our events. */
while (true) {
/* Get the signaled holder. */
os::MultiWaitHolderType *signaled_holder = os::WaitAny(std::addressof(multi_wait));
/* Process the holder. */
bool insert_change = false;
if (signaled_holder == std::addressof(detector_thread_end_holder)) {
/* We should kill ourselves. */
2021-10-09 22:59:59 +00:00
os::ClearEvent(std::addressof(m_detector_thread_end_event));
m_state = State_Finalized;
break;
} else if (signaled_holder == std::addressof(request_sleep_wake_event_holder)) {
/* A request for us to sleep/wake has come in, so we'll acknowledge it. */
2021-10-09 22:59:59 +00:00
os::ClearEvent(std::addressof(m_request_sleep_wake_event));
m_state = State_Sleep;
os::SignalEvent(std::addressof(m_acknowledge_sleep_awake_event));
/* Temporarily unlink our interrupt event. */
os::UnlinkMultiWaitHolder(std::addressof(gpio_event_holder));
/* Wait to be signaled. */
signaled_holder = os::WaitAny(std::addressof(multi_wait));
/* Link our interrupt event back in. */
os::LinkMultiWaitHolder(std::addressof(multi_wait), std::addressof(gpio_event_holder));
/* We're awake again. Either because we should exit, or because we were asked to wake up. */
2021-10-09 22:59:59 +00:00
os::ClearEvent(std::addressof(m_request_sleep_wake_event));
m_state = State_Awake;
os::SignalEvent(std::addressof(m_acknowledge_sleep_awake_event));
/* If we were asked to exit, do so. */
if (signaled_holder == std::addressof(detector_thread_end_holder)) {
/* We should kill ourselves. */
2021-10-09 22:59:59 +00:00
os::ClearEvent(std::addressof(m_detector_thread_end_event));
m_state = State_Finalized;
break;
} else /* if (signaled_holder == std::addressof(request_sleep_wake_event_holder)) */ {
2021-10-09 22:59:59 +00:00
if ((m_force_detection) ||
(({ bool active; R_SUCCEEDED(gpio::IsWakeEventActive(std::addressof(active), m_gpio_device_code)) && active; })) ||
(os::TryWaitSystemEvent(std::addressof(gpio_event))) ||
2021-10-09 22:59:59 +00:00
(m_is_prev_inserted != this->IsCurrentInserted()))
{
insert_change = true;
}
}
} else /* if (signaled_holder == std::addressof(gpio_event_holder)) */ {
/* An event was detected. */
insert_change = true;
}
/* Handle an insert change, if one occurred. */
if (insert_change) {
/* Call the relevant callback, if we have one. */
2021-10-09 22:59:59 +00:00
if (m_device_detection_event_callback != nullptr) {
m_device_detection_event_callback(m_device_detection_event_callback_arg);
}
/* Clear the interrupt event. */
os::ClearSystemEvent(std::addressof(gpio_event));
2021-10-09 22:59:59 +00:00
gpio::ClearInterruptStatus(std::addressof(m_gpio_pad_session));
gpio::SetInterruptEnable(std::addressof(m_gpio_pad_session), true);
/* Update insertion status. */
cur_inserted = this->IsCurrentInserted();
2021-10-09 22:59:59 +00:00
this->HandleDeviceStatus(m_is_prev_inserted, cur_inserted);
m_is_prev_inserted = cur_inserted;
}
}
/* Disable interrupts to our gpio event. */
2021-10-09 22:59:59 +00:00
gpio::SetInterruptEnable(std::addressof(m_gpio_pad_session), false);
/* Finalize and unlink multi wait/holders. */
os::UnlinkMultiWaitHolder(std::addressof(gpio_event_holder));
os::FinalizeMultiWaitHolder(std::addressof(gpio_event_holder));
os::UnlinkMultiWaitHolder(std::addressof(request_sleep_wake_event_holder));
os::FinalizeMultiWaitHolder(std::addressof(request_sleep_wake_event_holder));
os::UnlinkMultiWaitHolder(std::addressof(detector_thread_end_holder));
os::FinalizeMultiWaitHolder(std::addressof(detector_thread_end_holder));
os::FinalizeMultiWait(std::addressof(multi_wait));
/* Finalize the gpio session. */
2021-10-09 22:59:59 +00:00
gpio::UnbindInterrupt(std::addressof(m_gpio_pad_session));
gpio::CloseSession(std::addressof(m_gpio_pad_session));
gpio::Finalize();
}
void DeviceDetector::Initialize(CallbackInfo *ci) {
/* Transition our state from finalized to initializing. */
2021-10-09 22:59:59 +00:00
AMS_ABORT_UNLESS(m_state == State_Finalized);
m_state = State_Initializing;
/* Set our callback infos. */
2021-10-09 22:59:59 +00:00
m_callback_info = *ci;
/* Initialize our events. */
2021-10-09 22:59:59 +00:00
os::InitializeEvent(std::addressof(m_ready_device_status_event), false, os::EventClearMode_ManualClear);
os::InitializeEvent(std::addressof(m_request_sleep_wake_event), false, os::EventClearMode_ManualClear);
os::InitializeEvent(std::addressof(m_acknowledge_sleep_awake_event), false, os::EventClearMode_ManualClear);
os::InitializeEvent(std::addressof(m_detector_thread_end_event), false, os::EventClearMode_ManualClear);
/* Create and start the detector thread. */
2021-10-09 22:59:59 +00:00
os::CreateThread(std::addressof(m_detector_thread), DetectorThreadEntry, this, m_detector_thread_stack, sizeof(m_detector_thread_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(sdmmc, DeviceDetector));
os::SetThreadNamePointer(std::addressof(m_detector_thread), AMS_GET_SYSTEM_THREAD_NAME(sdmmc, DeviceDetector));
os::StartThread(std::addressof(m_detector_thread));
}
void DeviceDetector::Finalize() {
/* Ensure we're not already finalized. */
2021-10-09 22:59:59 +00:00
AMS_ABORT_UNLESS(m_state != State_Finalized);
/* Signal event to end the detector thread. */
2021-10-09 22:59:59 +00:00
os::SignalEvent(std::addressof(m_detector_thread_end_event));
os::WaitThread(std::addressof(m_detector_thread));
/* Finalize thread and events. */
2021-10-09 22:59:59 +00:00
os::DestroyThread(std::addressof(m_detector_thread));
os::FinalizeEvent(std::addressof(m_ready_device_status_event));
os::FinalizeEvent(std::addressof(m_request_sleep_wake_event));
os::FinalizeEvent(std::addressof(m_acknowledge_sleep_awake_event));
os::FinalizeEvent(std::addressof(m_detector_thread_end_event));
}
void DeviceDetector::PutToSleep() {
/* Signal request, wait for acknowledgement. */
2021-10-09 22:59:59 +00:00
os::SignalEvent(std::addressof(m_request_sleep_wake_event));
os::WaitEvent(std::addressof(m_acknowledge_sleep_awake_event));
os::ClearEvent(std::addressof(m_acknowledge_sleep_awake_event));
}
void DeviceDetector::Awaken(bool force_det) {
/* Signal request, wait for acknowledgement. */
2021-10-09 22:59:59 +00:00
m_force_detection = force_det;
os::SignalEvent(std::addressof(m_request_sleep_wake_event));
os::WaitEvent(std::addressof(m_acknowledge_sleep_awake_event));
os::ClearEvent(std::addressof(m_acknowledge_sleep_awake_event));
}
bool DeviceDetector::IsInserted() {
bool inserted = false;
2021-10-09 22:59:59 +00:00
switch (m_state) {
case State_Initializing:
/* Wait for us to know whether the device is inserted. */
2021-10-09 22:59:59 +00:00
os::WaitEvent(std::addressof(m_ready_device_status_event));
[[fallthrough]];
case State_Awake:
/* Get whether the device is currently inserted. */
inserted = this->IsCurrentInserted();
break;
case State_Sleep:
case State_Finalized:
/* Get whether the device was inserted when we last knew. */
2021-10-09 22:59:59 +00:00
inserted = m_is_prev_inserted;
break;
}
return inserted;
}
void DeviceDetector::RegisterDetectionEventCallback(DeviceDetectionEventCallback cb, void *arg) {
2021-10-09 22:59:59 +00:00
m_device_detection_event_callback_arg = arg;
m_device_detection_event_callback = cb;
}
void DeviceDetector::UnregisterDetectionEventCallback() {
2021-10-09 22:59:59 +00:00
m_device_detection_event_callback = nullptr;
}
}
#endif