sdmmc: add DeviceDetector, gpio: implement client api

This commit is contained in:
Michael Scire
2020-10-26 04:06:02 -07:00
parent 9a41e19004
commit 22fb4ff095
25 changed files with 950 additions and 42 deletions

View File

@@ -47,6 +47,7 @@
#include <stratosphere/erpt.hpp>
#include <stratosphere/err.hpp>
#include <stratosphere/fatal.hpp>
#include <stratosphere/gpio.hpp>
#include <stratosphere/hid.hpp>
#include <stratosphere/hos.hpp>
#include <stratosphere/kvdb.hpp>

View File

@@ -39,19 +39,21 @@ namespace ams::impl {
AMS_DEFINE_SYSTEM_THREAD(21, pm, Main);
AMS_DEFINE_SYSTEM_THREAD(21, pm, ProcessTrack);
/* NCM. */
AMS_DEFINE_SYSTEM_THREAD(21, ncm, MainWaitThreads);
AMS_DEFINE_SYSTEM_THREAD(21, ncm, ContentManagerServerIpcSession);
AMS_DEFINE_SYSTEM_THREAD(21, ncm, LocationResolverServerIpcSession);
/* FS. */
AMS_DEFINE_SYSTEM_THREAD(16, fs, WorkerThreadPool);
AMS_DEFINE_SYSTEM_THREAD(17, fs, Main);
AMS_DEFINE_SYSTEM_THREAD(17, fs, WorkerRealTimeAccess);
AMS_DEFINE_SYSTEM_THREAD(18, fs, WorkerNormalPriorityAccess);
AMS_DEFINE_SYSTEM_THREAD(19, fs, WorkerLowPriorityAccess);
AMS_DEFINE_SYSTEM_THREAD(30, fs, WorkerBackgroundAccess);
AMS_DEFINE_SYSTEM_THREAD(30, fs, PatrolReader);
AMS_DEFINE_SYSTEM_THREAD(11, sdmmc, DeviceDetector);
AMS_DEFINE_SYSTEM_THREAD(16, fs, WorkerThreadPool);
AMS_DEFINE_SYSTEM_THREAD(17, fs, Main);
AMS_DEFINE_SYSTEM_THREAD(17, fs, WorkerRealTimeAccess);
AMS_DEFINE_SYSTEM_THREAD(18, fs, WorkerNormalPriorityAccess);
AMS_DEFINE_SYSTEM_THREAD(19, fs, WorkerLowPriorityAccess);
AMS_DEFINE_SYSTEM_THREAD(30, fs, WorkerBackgroundAccess);
AMS_DEFINE_SYSTEM_THREAD(30, fs, PatrolReader);
/* Boot. */
AMS_DEFINE_SYSTEM_THREAD(-1, boot, Main);

View File

@@ -0,0 +1,19 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere/ddsf/ddsf_types.hpp>

View File

@@ -0,0 +1,30 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
namespace ams::ddsf {
enum AccessMode {
AccessMode_None = (0u << 0),
AccessMode_Read = (1u << 0),
AccessMode_Write = (1u << 1),
AccessMode_ReadWrite = AccessMode_Read | AccessMode_Write,
};
}

View File

@@ -0,0 +1,20 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere/gpio/gpio_types.hpp>
#include <stratosphere/gpio/gpio_api.hpp>
#include <stratosphere/gpio/gpio_pad_api.hpp>

View File

@@ -0,0 +1,25 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/gpio/gpio_types.hpp>
namespace ams::gpio {
void Initialize();
void Finalize();
}

View File

@@ -0,0 +1,58 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/ddsf/ddsf_types.hpp>
#include <stratosphere/gpio/gpio_types.hpp>
#include <stratosphere/gpio/gpio_select_pad_name.hpp>
namespace ams::gpio {
struct GpioPadSession {
void *_session;
os::SystemEventType *_event;
};
Result OpenSession(GpioPadSession *out_session, ams::DeviceCode device_code);
void CloseSession(GpioPadSession *session);
Direction GetDirection(GpioPadSession *session);
void SetDirection(GpioPadSession *session, Direction direction);
GpioValue GetValue(GpioPadSession *session);
void SetValue(GpioPadSession *session, GpioValue value);
InterruptMode GetInterruptMode(GpioPadSession *session);
void SetInterruptMode(GpioPadSession *session, InterruptMode mode);
bool GetInterruptEnable(GpioPadSession *session);
void SetInterruptEnable(GpioPadSession *session, bool en);
InterruptStatus GetInterruptStatus(GpioPadSession *session);
void ClearInterruptStatus(GpioPadSession *session);
int GetDebounceTime(GpioPadSession *session);
void SetDebounceTime(GpioPadSession *session, int ms);
bool GetDebounceEnabled(GpioPadSession *session);
void SetDebounceEnabled(GpioPadSession *session, bool en);
Result BindInterrupt(os::SystemEventType *event, GpioPadSession *session);
void UnbindInterrupt(GpioPadSession *session);
Result IsWakeEventActive(bool *out_is_active, ams::DeviceCode device_code);
}

View File

@@ -0,0 +1,55 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/gpio/gpio_types.hpp>
namespace ams::gpio {
enum GpioPadName {
GpioPadName_CodecLdoEnTemp = 1,
GpioPadName_ButtonVolUp = 25,
GpioPadName_ButtonVolDn = 26,
GpioPadName_SdCd = 56,
};
/* TODO: Better place for this? */
constexpr inline const DeviceCode DeviceCode_CodecLdoEnTemp = 0x33000002;
constexpr inline const DeviceCode DeviceCode_ButtonVolUp = 0x35000002;
constexpr inline const DeviceCode DeviceCode_ButtonVolDn = 0x35000003;
constexpr inline const DeviceCode DeviceCode_SdCd = 0x3C000002;
constexpr inline GpioPadName ConvertToGpioPadName(DeviceCode dc) {
switch (dc.GetInternalValue()) {
case DeviceCode_CodecLdoEnTemp.GetInternalValue(): return GpioPadName_CodecLdoEnTemp;
case DeviceCode_ButtonVolUp .GetInternalValue(): return GpioPadName_ButtonVolUp;
case DeviceCode_ButtonVolDn .GetInternalValue(): return GpioPadName_ButtonVolDn;
case DeviceCode_SdCd .GetInternalValue(): return GpioPadName_SdCd;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
constexpr inline DeviceCode ConvertToDeviceCode(GpioPadName gpn) {
switch (gpn) {
case GpioPadName_CodecLdoEnTemp: return DeviceCode_CodecLdoEnTemp;
case GpioPadName_ButtonVolUp: return DeviceCode_ButtonVolUp;
case GpioPadName_ButtonVolDn: return DeviceCode_ButtonVolDn;
case GpioPadName_SdCd: return DeviceCode_SdCd;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
}

View File

@@ -0,0 +1,24 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/gpio/gpio_types.hpp>
#if defined(ATMOSPHERE_BOARD_NINTENDO_NX)
#include <stratosphere/gpio/gpio_pad_name.board.nintendo_nx.hpp>
#else
/* Error? */
#endif

View File

@@ -0,0 +1,44 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
namespace ams::gpio {
enum InterruptMode {
InterruptMode_LowLevel = 0,
InterruptMode_HighLevel = 1,
InterruptMode_RisingEdge = 2,
InterruptMode_FallingEdge = 3,
InterruptMode_AnyEdge = 4,
};
enum Direction {
Direction_Input = 0,
Direction_Output = 1,
};
enum GpioValue {
GpioValue_Low = 0,
GpioValue_High = 1,
};
enum InterruptStatus {
InterruptStatus_Inactive = 0,
InterruptStatus_Active = 1,
};
}

View File

@@ -167,20 +167,19 @@ namespace ams::boot2 {
}
}
bool GetGpioPadLow(GpioPadName pad) {
GpioPadSession button;
if (R_FAILED(gpioOpenSession(&button, pad))) {
bool GetGpioPadLow(DeviceCode device_code) {
gpio::GpioPadSession button;
if (R_FAILED(gpio::OpenSession(std::addressof(button), device_code))) {
return false;
}
/* Ensure we close even on early return. */
ON_SCOPE_EXIT { gpioPadClose(&button); };
ON_SCOPE_EXIT { gpio::CloseSession(std::addressof(button)); };
/* Set direction input. */
gpioPadSetDirection(&button, GpioDirection_Input);
gpio::SetDirection(std::addressof(button), gpio::Direction_Input);
GpioValue val;
return R_SUCCEEDED(gpioPadGetValue(&button, &val)) && val == GpioValue_Low;
return gpio::GetValue(std::addressof(button)) == gpio::GpioValue_Low;
}
bool IsForceMaintenance() {
@@ -197,7 +196,7 @@ namespace ams::boot2 {
/* Contact GPIO, read plus/minus buttons. */
{
return GetGpioPadLow(GpioPadName_ButtonVolUp) && GetGpioPadLow(GpioPadName_ButtonVolDown);
return GetGpioPadLow(gpio::DeviceCode_ButtonVolUp) && GetGpioPadLow(gpio::DeviceCode_ButtonVolDn);
}
}

View File

@@ -0,0 +1,190 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace ams::gpio {
/* TODO: This deserves proper new interface def support. */
namespace {
/* TODO: Manager object. */
constinit os::SdkMutex g_init_mutex;
constinit int g_initialize_count = 0;
using InternalSession = ::GpioPadSession;
InternalSession *GetInterface(GpioPadSession *session) {
AMS_ASSERT(session->_session != nullptr);
return static_cast<::GpioPadSession *>(session->_session);
}
}
void Initialize() {
std::scoped_lock lk(g_init_mutex);
if ((g_initialize_count++) == 0) {
R_ABORT_UNLESS(::gpioInitialize());
}
}
void Finalize() {
std::scoped_lock lk(g_init_mutex);
AMS_ASSERT(g_initialize_count > 0);
if ((--g_initialize_count) == 0) {
::gpioExit();
}
}
Result OpenSession(GpioPadSession *out_session, ams::DeviceCode device_code) {
/* Allocate the session. */
InternalSession *internal_session = static_cast<InternalSession *>(std::malloc(sizeof(InternalSession)));
AMS_ABORT_UNLESS(internal_session != nullptr);
auto session_guard = SCOPE_GUARD { std::free(internal_session); };
/* Get the session. */
if (hos::GetVersion() >= hos::Version_7_0_0) {
R_TRY(::gpioOpenSession2(internal_session, device_code.GetInternalValue(), ddsf::AccessMode_ReadWrite));
} else {
R_ABORT_UNLESS(::gpioOpenSession(internal_session, static_cast<::GpioPadName>(static_cast<s32>(ConvertToGpioPadName(device_code)))));
}
/* Set output. */
out_session->_session = internal_session;
out_session->_event = nullptr;
/* We succeeded. */
session_guard.Cancel();
return ResultSuccess();
}
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. */
::gpioPadClose(GetInterface(session));
std::free(session->_session);
session->_session = nullptr;
}
Result IsWakeEventActive(bool *out_is_active, ams::DeviceCode device_code) {
if (hos::GetVersion() >= hos::Version_7_0_0) {
R_TRY(::gpioIsWakeEventActive2(out_is_active, device_code.GetInternalValue()));
} else {
R_ABORT_UNLESS(::gpioIsWakeEventActive(out_is_active, static_cast<::GpioPadName>(static_cast<s32>(ConvertToGpioPadName(device_code)))));
}
return ResultSuccess();
}
Direction GetDirection(GpioPadSession *session) {
::GpioDirection out;
R_ABORT_UNLESS(::gpioPadGetDirection(GetInterface(session), std::addressof(out)));
return static_cast<Direction>(static_cast<s32>(out));
}
void SetDirection(GpioPadSession *session, Direction direction) {
R_ABORT_UNLESS(::gpioPadSetDirection(GetInterface(session), static_cast<::GpioDirection>(static_cast<s32>(direction))));
}
GpioValue GetValue(GpioPadSession *session) {
::GpioValue out;
R_ABORT_UNLESS(::gpioPadGetValue(GetInterface(session), std::addressof(out)));
return static_cast<GpioValue>(static_cast<s32>(out));
}
void SetValue(GpioPadSession *session, GpioValue value) {
R_ABORT_UNLESS(::gpioPadSetValue(GetInterface(session), static_cast<::GpioValue>(static_cast<s32>(value))));
}
InterruptMode GetInterruptMode(GpioPadSession *session) {
::GpioInterruptMode out;
R_ABORT_UNLESS(::gpioPadGetInterruptMode(GetInterface(session), std::addressof(out)));
return static_cast<InterruptMode>(static_cast<s32>(out));
}
void SetInterruptMode(GpioPadSession *session, InterruptMode mode) {
R_ABORT_UNLESS(::gpioPadSetInterruptMode(GetInterface(session), static_cast<::GpioInterruptMode>(static_cast<s32>(mode))));
}
bool GetInterruptEnable(GpioPadSession *session) {
bool out;
R_ABORT_UNLESS(::gpioPadGetInterruptEnable(GetInterface(session), std::addressof(out)));
return out;
}
void SetInterruptEnable(GpioPadSession *session, bool en) {
R_ABORT_UNLESS(::gpioPadSetInterruptEnable(GetInterface(session), en));
}
InterruptStatus GetInterruptStatus(GpioPadSession *session) {
::GpioInterruptStatus out;
R_ABORT_UNLESS(::gpioPadGetInterruptStatus(GetInterface(session), std::addressof(out)));
return static_cast<InterruptStatus>(static_cast<s32>(out));
}
void ClearInterruptStatus(GpioPadSession *session) {
R_ABORT_UNLESS(::gpioPadClearInterruptStatus(GetInterface(session)));
}
int GetDebounceTime(GpioPadSession *session) {
int out;
R_ABORT_UNLESS(::gpioPadGetDebounceTime(GetInterface(session), std::addressof(out)));
return out;
}
void SetDebounceTime(GpioPadSession *session, int ms) {
R_ABORT_UNLESS(::gpioPadSetDebounceTime(GetInterface(session), ms));
}
bool GetDebounceEnabled(GpioPadSession *session) {
bool out;
R_ABORT_UNLESS(::gpioPadGetDebounceEnabled(GetInterface(session), std::addressof(out)));
return out;
}
void SetDebounceEnabled(GpioPadSession *session, bool en) {
R_ABORT_UNLESS(::gpioPadSetDebounceEnabled(GetInterface(session), en));
}
Result BindInterrupt(os::SystemEventType *event, GpioPadSession *session) {
::Event ev;
R_TRY(::gpioPadBindInterrupt(GetInterface(session), std::addressof(ev)));
os::AttachReadableHandleToSystemEvent(event, ev.revent, true, os::EventClearMode_ManualClear);
session->_event = event;
return ResultSuccess();
}
void UnbindInterrupt(GpioPadSession *session) {
AMS_ASSERT(session->_event != nullptr);
R_ABORT_UNLESS(::gpioPadUnbindInterrupt(GetInterface(session)));
os::DestroySystemEvent(session->_event);
session->_event = nullptr;
}
}