gpio: implement more of server library for boot sysmodule client usage

This commit is contained in:
Michael Scire 2020-10-31 03:22:01 -07:00 committed by SciresM
parent e1b5d81d65
commit bd3ab76fd2
26 changed files with 1162 additions and 24 deletions

View File

@ -59,7 +59,7 @@ namespace ams::ddsf {
template<typename T>
constexpr const T &SafeCastTo() const {
this->AssertCastableTo<T>();
return static_cast<T &>(*this);
return static_cast<const T &>(*this);
}
template<typename T>
@ -71,7 +71,7 @@ namespace ams::ddsf {
template<typename T>
constexpr const T *SafeCastToPointer() const {
this->AssertCastableTo<T>();
return static_cast<T *>(this);
return static_cast<const T *>(this);
}
#if defined(AMS_BUILD_FOR_AUDITING) || defined(AMS_BUILD_FOR_DEBUGGING)

View File

@ -21,3 +21,6 @@
#include <stratosphere/gpio/gpio_api.hpp>
#include <stratosphere/gpio/gpio_pad_api.hpp>
#include <stratosphere/gpio/driver/gpio_select_driver_api.hpp>
#include <stratosphere/gpio/driver/gpio_pad_accessor.hpp>
#include <stratosphere/gpio/driver/impl/gpio_pad_session_impl.hpp>
#include <stratosphere/gpio/server/gpio_server_api.hpp>

View File

@ -23,7 +23,7 @@ namespace ams::gpio::driver {
class Pad : public ::ams::ddsf::IDevice {
NON_COPYABLE(Pad);
NON_MOVEABLE(Pad);
AMS_DDSF_CASTABLE_TRAITS(ams::gpio::Pad, ::ams::ddsf::IDevice);
AMS_DDSF_CASTABLE_TRAITS(ams::gpio::driver::Pad, ::ams::ddsf::IDevice);
private:
int pad_number;
bool is_interrupt_enabled;

View File

@ -0,0 +1,45 @@
/*
* 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::driver {
namespace impl {
constexpr inline size_t GpioPadSessionSize = 0x60;
constexpr inline size_t GpioPadSessionAlign = 8;
struct alignas(GpioPadSessionAlign) GpioPadSessionImplPadded;
}
struct GpioPadSession {
util::TypedStorage<impl::GpioPadSessionImplPadded, impl::GpioPadSessionSize, impl::GpioPadSessionAlign> _impl;
};
Result OpenSession(GpioPadSession *out, DeviceCode device_code);
void CloseSession(GpioPadSession *session);
Result SetDirection(GpioPadSession *session, gpio::Direction direction);
Result GetDirection(gpio::Direction *out, GpioPadSession *session);
Result SetValue(GpioPadSession *session, gpio::GpioValue value);
Result GetValue(gpio::GpioValue *out, GpioPadSession *session);
/* TODO */
}

View File

@ -0,0 +1,49 @@
/*
* 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/os.hpp>
namespace ams::gpio::driver::impl {
class EventHolder {
NON_COPYABLE(EventHolder);
NON_MOVEABLE(EventHolder);
private:
os::SystemEventType *event;
public:
constexpr EventHolder() : event(nullptr) { /* ... */ }
void AttachEvent(os::SystemEventType *event) {
this->event = event;
}
os::SystemEventType *DetachEvent() {
auto ev = this->event;
this->event = nullptr;
return ev;
}
os::SystemEventType *GetSystemEvent() {
return this->event;
}
bool IsBound() const {
return this->event != nullptr;
}
};
}

View File

@ -0,0 +1,90 @@
/*
* 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>
#include <stratosphere/gpio/driver/gpio_pad_accessor.hpp>
#include <stratosphere/gpio/driver/impl/gpio_event_holder.hpp>
#include <stratosphere/ddsf.hpp>
namespace ams::gpio::driver {
class Pad;
}
namespace ams::gpio::driver::impl {
class PadSessionImpl : public ::ams::ddsf::ISession {
NON_COPYABLE(PadSessionImpl);
NON_MOVEABLE(PadSessionImpl);
AMS_DDSF_CASTABLE_TRAITS(ams::gpio::driver::impl::PadSessionImpl, ::ams::ddsf::ISession);
private:
EventHolder event_holder;
private:
Result UpdateDriverInterruptEnabled();
public:
PadSessionImpl() : event_holder() { /* ... */ }
~PadSessionImpl() {
this->Close();
}
bool IsInterruptBound() const {
return this->event_holder.IsBound();
}
Result Open(Pad *pad, ddsf::AccessMode access_mode);
void Close();
Result BindInterrupt(os::SystemEventType *event);
void UnbindInterrupt();
Result GetInterruptEnabled(bool *out) const;
Result SetInterruptEnabled(bool en);
void SignalInterruptBoundEvent();
};
static_assert( sizeof(PadSessionImpl) <= GpioPadSessionSize);
static_assert(alignof(PadSessionImpl) <= GpioPadSessionAlign);
struct alignas(GpioPadSessionAlign) GpioPadSessionImplPadded {
PadSessionImpl _impl;
u8 _padding[GpioPadSessionSize - sizeof(PadSessionImpl)];
};
static_assert( sizeof(GpioPadSessionImplPadded) == GpioPadSessionSize);
static_assert(alignof(GpioPadSessionImplPadded) == GpioPadSessionAlign);
ALWAYS_INLINE PadSessionImpl &GetPadSessionImpl(GpioPadSession &session) {
return GetReference(session._impl)._impl;
}
ALWAYS_INLINE const PadSessionImpl &GetPadSessionImpl(const GpioPadSession &session) {
return GetReference(session._impl)._impl;
}
ALWAYS_INLINE PadSessionImpl &GetOpenPadSessionImpl(GpioPadSession &session) {
auto &ref = GetReference(session._impl)._impl;
AMS_ASSERT(ref.IsOpen());
return ref;
}
ALWAYS_INLINE const PadSessionImpl &GetOpenPadSessionImpl(const GpioPadSession &session) {
const auto &ref = GetReference(session._impl)._impl;
AMS_ASSERT(ref.IsOpen());
return ref;
}
}

View File

@ -16,10 +16,13 @@
#pragma once
#include <vapours.hpp>
#include <stratosphere/gpio/gpio_types.hpp>
#include <stratosphere/gpio/sf/gpio_sf_i_manager.hpp>
namespace ams::gpio {
void Initialize();
void Finalize();
void InitializeWith(std::shared_ptr<gpio::sf::IManager> &&sp);
}

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>
#include <stratosphere/gpio/sf/gpio_sf_i_manager.hpp>
namespace ams::gpio::server {
std::shared_ptr<gpio::sf::IManager> GetServiceObject();
}

View File

@ -0,0 +1,96 @@
/*
* 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/lmem.hpp>
#include <stratosphere/sf/sf_lmem_utility.hpp>
#include <stratosphere/gpio/sf/gpio_sf_i_manager.hpp>
namespace ams::gpio::server {
class ManagerImpl {
private:
ams::sf::ExpHeapMemoryResource pad_session_memory_resource;
typename ams::sf::ServiceObjectAllocator<gpio::sf::IPadSession>
public:
ManagerImpl();
~ManagerImpl();
public:
/* Actual commands. */
Result OpenSessionForDev(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, s32 pad_descriptor) {
/* TODO: libnx bindings */
AMS_ABORT();
}
Result OpenSession(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, gpio::GpioPadName pad_name) {
::GpioPadSession p;
R_TRY(::gpioOpenSession(std::addressof(p), static_cast<::GpioPadName>(static_cast<u32>(pad_name))));
out.SetValue(ams::sf::MakeShared<gpio::sf::IPadSession, RemotePadSessionImpl>(p));
return ResultSuccess();
}
Result OpenSessionForTest(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, gpio::GpioPadName pad_name) {
/* TODO: libnx bindings */
AMS_ABORT();
}
Result IsWakeEventActive(ams::sf::Out<bool> out, gpio::GpioPadName pad_name) {
return ::gpioIsWakeEventActive2(out.GetPointer(), static_cast<::GpioPadName>(static_cast<u32>(pad_name)));
}
Result GetWakeEventActiveFlagSet(ams::sf::Out<gpio::WakeBitFlag> out) {
/* TODO: libnx bindings */
AMS_ABORT();
}
Result SetWakeEventActiveFlagSetForDebug(gpio::GpioPadName pad_name, bool is_enabled) {
/* TODO: libnx bindings */
AMS_ABORT();
}
Result SetWakePinDebugMode(s32 mode) {
/* TODO: libnx bindings */
AMS_ABORT();
}
Result OpenSession2(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, DeviceCode device_code, ddsf::AccessMode access_mode) {
::GpioPadSession p;
R_TRY(::gpioOpenSession2(std::addressof(p), device_code.GetInternalValue(), access_mode));
out.SetValue(ams::sf::MakeShared<gpio::sf::IPadSession, RemotePadSessionImpl>(p));
return ResultSuccess();
}
Result IsWakeEventActive2(ams::sf::Out<bool> out, DeviceCode device_code) {
return ::gpioIsWakeEventActive2(out.GetPointer(), device_code.GetInternalValue());
}
Result SetWakeEventActiveFlagSetForDebug2(DeviceCode device_code, bool is_enabled) {
/* TODO: libnx bindings */
AMS_ABORT();
}
Result SetRetryValues(u32 arg0, u32 arg1) {
/* TODO: libnx bindings */
AMS_ABORT();
}
};
static_assert(gpio::sf::IsIManager<ManagerImpl>);
}

View File

@ -24,7 +24,13 @@ namespace ams::sf {
private:
lmem::HeapHandle handle;
public:
explicit ExpHeapMemoryResource(lmem::HeapHandle h) : handle(h) { /* ... */ }
constexpr ExpHeapMemoryResource() : handle() { /* ... */ }
constexpr explicit ExpHeapMemoryResource(lmem::HeapHandle h) : handle(h) { /* ... */ }
void Attach(lmem::HeapHandle h) {
AMS_ABORT_UNLESS(this->handle == lmem::HeapHandle());
this->handle = h;
}
lmem::HeapHandle GetHandle() const { return this->handle; }
private:
@ -45,7 +51,13 @@ namespace ams::sf {
private:
lmem::HeapHandle handle;
public:
explicit UnitHeapMemoryResource(lmem::HeapHandle h) : handle(h) { /* ... */ }
constexpr UnitHeapMemoryResource() : handle() { /* ... */ }
constexpr explicit UnitHeapMemoryResource(lmem::HeapHandle h) : handle(h) { /* ... */ }
void Attach(lmem::HeapHandle h) {
AMS_ABORT_UNLESS(this->handle == lmem::HeapHandle());
this->handle = h;
}
lmem::HeapHandle GetHandle() const { return this->handle; }
private:

View File

@ -62,6 +62,40 @@ namespace ams::sf {
return std::make_shared<typename Interface::ImplSharedPointer<Impl>>(std::make_shared<Impl>(std::forward<Arguments>(args)...));
}
template<typename Interface, typename Impl>
class ServiceObjectAllocator {
public:
using value_type = typename Interface::ImplHolder<Impl>;
private:
MemoryResource * const memory_resource;
public:
constexpr ServiceObjectAllocator(MemoryResource *mr) : memory_resource(mr) { /* ... */ }
value_type *allocate(size_t n) const {
void *mem = this->memory_resource->Allocate(n * sizeof(value_type), alignof(value_type));
AMS_ABORT_UNLESS(mem != nullptr);
return mem;
}
void deallocate(void *p, size_t n) const {
this->memory_resource->Deallocate(p, n * sizeof(value_type), alignof(value_type));
}
inline bool operator==(const ServiceObjectAllocator &rhs) const {
return this->memory_resource->is_equal(*rhs->memory_resource);
}
inline bool operator!=(const ServiceObjectAllocator &rhs) const {
return !(*this == rhs);
}
};
template<typename Interface, typename Impl, typename Allocator, typename... Arguments>
requires std::constructible_from<Impl, Arguments...>
constexpr ALWAYS_INLINE std::shared_ptr<typename Interface::ImplHolder<Impl>> AllocateShared(const Allocator &allocator, Arguments &&... args) {
return std::allocate_shared<typename Interface::ImplHolder<Impl>>(allocator, std::forward<Arguments>(args)...);
}
template<typename Interface, typename Impl>
constexpr ALWAYS_INLINE std::shared_ptr<typename Interface::ImplPointer<Impl>> GetSharedPointerTo(Impl *impl) {
return std::make_shared<typename Interface::ImplPointer<Impl>>(impl);
@ -72,4 +106,4 @@ namespace ams::sf {
return GetSharedPointerTo<Interface, Impl>(std::addressof(impl));
}
}
}

View File

@ -19,6 +19,71 @@
namespace ams::gpio::driver::board::nintendo_nx::impl {
void InterruptEventHandler::Initialize(DriverImpl *drv, os::InterruptName intr, int ctlr) {
/* Set fields. */
this->driver = drv;
this->interrupt_name = intr;
this->controller_number = ctlr;
/* Initialize interrupt event. */
os::InitializeInterruptEvent(std::addressof(this->interrupt_event), intr, os::EventClearMode_ManualClear);
/* Initialize base. */
IEventHandler::Initialize(std::addressof(this->interrupt_event));
}
void InterruptEventHandler::HandleEvent() {
/* Lock the driver's interrupt mutex. */
std::scoped_lock lk(this->driver->interrupt_control_mutex);
/* Check each pad. */
bool found = false;
for (auto it = this->driver->interrupt_pad_list.begin(); !found && it != this->driver->interrupt_pad_list.end(); ++it) {
found = this->CheckAndHandleInterrupt(*it);
}
/* If we didn't find a pad, clear the interrupt event. */
if (!found) {
os::ClearInterruptEvent(std::addressof(this->interrupt_event));
}
}
bool InterruptEventHandler::CheckAndHandleInterrupt(TegraPad &pad) {
/* Get the pad's number. */
const InternalGpioPadNumber pad_number = static_cast<InternalGpioPadNumber>(pad.GetPadNumber());
/* Check if the pad matches our controller number. */
if (this->controller_number != ConvertInternalGpioPadNumberToController(pad_number)) {
return false;
}
/* Get the addresses of INT_STA, INT_ENB. */
const uintptr_t sta_address = GetGpioRegisterAddress(this->driver->gpio_virtual_address, GpioRegisterType_GPIO_INT_STA, pad_number);
const uintptr_t enb_address = GetGpioRegisterAddress(this->driver->gpio_virtual_address, GpioRegisterType_GPIO_INT_STA, pad_number);
const uintptr_t pad_index = ConvertInternalGpioPadNumberToBitIndex(pad_number);
/* Check if both STA and ENB are set. */
if (reg::Read(sta_address, 1u << pad_index) == 0 || reg::Read(enb_address, 1u << pad_index) == 0) {
return false;
}
/* The pad is signaled. First, clear the enb bit. */
SetMaskedBit(enb_address, pad_index, 0);
reg::Read(enb_address);
/* Disable the interrupt on the pad. */
pad.SetInterruptEnabled(false);
this->driver->RemoveInterruptPad(std::addressof(pad));
/* Clear the interrupt event. */
os::ClearInterruptEvent(std::addressof(this->interrupt_event));
/* Signal the pad's bound event. */
pad.SignalInterruptBoundEvent();
return true;
}
DriverImpl::DriverImpl(dd::PhysicalAddress reg_paddr, size_t size) : gpio_physical_address(reg_paddr), gpio_virtual_address(), suspend_handler(this), interrupt_pad_list(), interrupt_control_mutex() {
/* Get the corresponding virtual address for our physical address. */
this->gpio_virtual_address = dd::QueryIoMapping(reg_paddr, size);

View File

@ -29,13 +29,13 @@ namespace ams::gpio::driver::board::nintendo_nx::impl {
DriverImpl *driver;
os::InterruptName interrupt_name;
os::InterruptEventType interrupt_event;
int port_number;
int controller_number;
private:
void CheckAndHandleInterrupt(TegraPad *pad);
bool CheckAndHandleInterrupt(TegraPad &pad);
public:
InterruptEventHandler() : IEventHandler(), driver(nullptr), interrupt_name(), interrupt_event(), port_number() { /* ... */ }
InterruptEventHandler() : IEventHandler(), driver(nullptr), interrupt_name(), interrupt_event(), controller_number() { /* ... */ }
void Initialize(DriverImpl *driver, os::InterruptName intr, int port);
void Initialize(DriverImpl *drv, os::InterruptName intr, int ctlr);
virtual void HandleEvent() override;
};
@ -44,11 +44,12 @@ namespace ams::gpio::driver::board::nintendo_nx::impl {
NON_COPYABLE(DriverImpl);
NON_MOVEABLE(DriverImpl);
AMS_DDSF_CASTABLE_TRAITS(ams::gpio::driver::board::nintendo_nx::impl::DriverImpl, ::ams::gpio::driver::IGpioDriver);
friend class InterruptEventHandler;
private:
dd::PhysicalAddress gpio_physical_address;
uintptr_t gpio_virtual_address;
SuspendHandler suspend_handler;
TegraPad::List interrupt_pad_list;
TegraPad::InterruptList interrupt_pad_list;
mutable os::SdkMutex interrupt_control_mutex;
public:
DriverImpl(dd::PhysicalAddress reg_paddr, size_t size);
@ -96,6 +97,33 @@ namespace ams::gpio::driver::board::nintendo_nx::impl {
virtual Result SuspendLow() override;
virtual Result Resume() override;
virtual Result ResumeLow() override;
private:
static constexpr ALWAYS_INLINE TegraPad &GetTegraPad(Pad *pad) {
AMS_ASSERT(pad != nullptr);
return static_cast<TegraPad &>(*pad);
}
static ALWAYS_INLINE const PadInfo &GetInfo(Pad *pad) {
return GetTegraPad(pad).GetInfo();
}
static ALWAYS_INLINE PadStatus &GetStatus(Pad *pad) {
return GetTegraPad(pad).GetStatus();
}
void AddInterruptPad(TegraPad *pad) {
AMS_ASSERT(pad != nullptr);
if (!pad->IsLinkedToInterruptBoundPadList()) {
this->interrupt_pad_list.push_back(*pad);
}
}
void RemoveInterruptPad(TegraPad *pad) {
AMS_ASSERT(pad != nullptr);
if (pad->IsLinkedToInterruptBoundPadList()) {
this->interrupt_pad_list.erase(this->interrupt_pad_list.iterator_to(*pad));
}
}
};
}

View File

@ -0,0 +1,48 @@
/*
* 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>
#include "impl/gpio_driver_core.hpp"
namespace ams::gpio::driver {
bool Pad::IsAnySessionBoundToInterrupt() const {
/* Check to see if any session has an interrupt bound. */
bool bound = false;
this->ForEachSession([&](const ddsf::ISession &session) -> bool {
const auto &impl = session.SafeCastTo<impl::PadSessionImpl>();
if (impl.IsInterruptBound()) {
bound = true;
return false;
}
return true;
});
return bound;
}
void Pad::SignalInterruptBoundEvent() {
/* Signal relevant sessions. */
this->ForEachSession([&](ddsf::ISession &session) -> bool {
auto &impl = session.SafeCastTo<impl::PadSessionImpl>();
if (impl.IsInterruptBound()) {
impl.SignalInterruptBoundEvent();
}
return true;
});
}
}

View File

@ -0,0 +1,150 @@
/*
* 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::driver::impl {
Result PadSessionImpl::Open(Pad *pad, ddsf::AccessMode access_mode) {
/* Check if the pad has any open sessions. */
const bool first_session = !pad->HasAnyOpenSession();
/* Open the session. */
R_TRY(ddsf::OpenSession(pad, this, access_mode));
auto pad_guard = SCOPE_GUARD { ddsf::CloseSession(this); };
/* If we're the first, we want to initialize the pad. */
if (first_session) {
R_TRY(pad->GetDriver().SafeCastTo<IGpioDriver>().InitializePad(pad));
}
/* We opened successfully. */
pad_guard.Cancel();
return ResultSuccess();
}
void PadSessionImpl::Close() {
/* If the session isn't open, nothing to do. */
if (!this->IsOpen()) {
return;
}
/* Unbind the interrupt, if it's bound. */
if (this->IsInterruptBound()) {
this->UnbindInterrupt();
}
/* Get the pad we're a session for. */
auto &pad = this->GetDevice().SafeCastTo<Pad>();
/* Close the session. */
ddsf::CloseSession(this);
/* If we were the last session on the pad, finalize the pad. */
if (!pad.HasAnyOpenSession()) {
pad.GetDriver().SafeCastTo<IGpioDriver>().FinalizePad(std::addressof(pad));
}
}
Result PadSessionImpl::BindInterrupt(os::SystemEventType *event) {
/* Acquire exclusive access to the relevant interrupt control mutex. */
auto &pad = this->GetDevice().SafeCastTo<Pad>();
auto &mutex = pad.GetDriver().SafeCastTo<IGpioDriver>().GetInterruptControlMutex(pad);
std::scoped_lock lk(mutex);
/* Check that we're not already bound. */
R_UNLESS(!this->IsInterruptBound(), gpio::ResultAlreadyBound());
R_UNLESS(!this->GetDevice().SafeCastTo<Pad>().IsAnySessionBoundToInterrupt(), gpio::ResultAlreadyBound());
/* Create the system event. */
R_TRY(os::CreateSystemEvent(event, os::EventClearMode_ManualClear, true));
auto ev_guard = SCOPE_GUARD { os::DestroySystemEvent(event); };
/* Attach the event to our holder. */
this->event_holder.AttachEvent(event);
auto hl_guard = SCOPE_GUARD { this->event_holder.DetachEvent(); };
/* Update interrupt needed. */
R_TRY(this->UpdateDriverInterruptEnabled());
/* We succeeded. */
hl_guard.Cancel();
ev_guard.Cancel();
return ResultSuccess();
}
void PadSessionImpl::UnbindInterrupt() {
/* Acquire exclusive access to the relevant interrupt control mutex. */
auto &pad = this->GetDevice().SafeCastTo<Pad>();
auto &mutex = pad.GetDriver().SafeCastTo<IGpioDriver>().GetInterruptControlMutex(pad);
std::scoped_lock lk(mutex);
/* If we're not bound, nothing to do. */
if (!this->IsInterruptBound()) {
return;
}
/* Detach and destroy the event */
os::DestroySystemEvent(this->event_holder.DetachEvent());
/* Update interrupt needed. */
R_ABORT_UNLESS(this->UpdateDriverInterruptEnabled());
}
Result PadSessionImpl::UpdateDriverInterruptEnabled() {
/* Check we have exclusive access to the relevant interrupt control mutex. */
auto &pad = this->GetDevice().SafeCastTo<Pad>();
auto &driver = pad.GetDriver().SafeCastTo<IGpioDriver>();
AMS_ASSERT(driver.GetInterruptControlMutex(pad).IsLockedByCurrentThread());
/* Set interrupt enabled. */
return driver.SetInterruptEnabled(std::addressof(pad), pad.IsInterruptRequiredForDriver());
}
Result PadSessionImpl::GetInterruptEnabled(bool *out) const {
*out = this->GetDevice().SafeCastTo<Pad>().IsInterruptEnabled();
return ResultSuccess();
}
Result PadSessionImpl::SetInterruptEnabled(bool en) {
/* Acquire exclusive access to the relevant interrupt control mutex. */
auto &pad = this->GetDevice().SafeCastTo<Pad>();
auto &mutex = pad.GetDriver().SafeCastTo<IGpioDriver>().GetInterruptControlMutex(pad);
std::scoped_lock lk(mutex);
/* Set the interrupt enable. */
const bool prev = pad.IsInterruptEnabled();
pad.SetInterruptEnabled(en);
auto pad_guard = SCOPE_GUARD { pad.SetInterruptEnabled(prev); };
/* Update interrupt needed. */
R_TRY(this->UpdateDriverInterruptEnabled());
pad_guard.Cancel();
return ResultSuccess();
}
void PadSessionImpl::SignalInterruptBoundEvent() {
/* Check we have exclusive access to the relevant interrupt control mutex. */
auto &pad = this->GetDevice().SafeCastTo<Pad>();
auto &driver = pad.GetDriver().SafeCastTo<IGpioDriver>();
AMS_ASSERT(driver.GetInterruptControlMutex(pad).IsLockedByCurrentThread());
if (auto *event = this->event_holder.GetSystemEvent(); event != nullptr) {
os::SignalSystemEvent(event);
}
}
}

View File

@ -23,6 +23,7 @@ namespace ams::gpio {
/* TODO: Manager object. */
constinit os::SdkMutex g_init_mutex;
constinit int g_initialize_count = 0;
constinit bool g_remote = false;
std::shared_ptr<sf::IManager> g_manager;
using InternalSession = std::shared_ptr<gpio::sf::IPadSession>;
@ -40,9 +41,19 @@ namespace ams::gpio {
if ((g_initialize_count++) == 0) {
R_ABORT_UNLESS(::gpioInitialize());
g_manager = ams::sf::MakeShared<sf::IManager, RemoteManagerImpl>();
g_remote = true;
}
}
void InitializeWith(std::shared_ptr<gpio::sf::IManager> &&sp) {
std::scoped_lock lk(g_init_mutex);
AMS_ABORT_UNLESS(g_initialize_count == 0);
g_manager = std::move(sp);
g_initialize_count = 1;
}
void Finalize() {
std::scoped_lock lk(g_init_mutex);
@ -50,7 +61,9 @@ namespace ams::gpio {
if ((--g_initialize_count) == 0) {
g_manager.reset();
::gpioExit();
if (g_remote) {
::gpioExit();
}
}
}

View File

@ -0,0 +1,36 @@
/*
* 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>
#include "gpio_server_manager_impl.hpp"
namespace ams::gpio::server {
namespace {
ManagerImpl g_manager_impl;
std::shared_ptr<gpio::sf::IManager> GetManagerServiceObject() {
static std::shared_ptr<gpio::sf::IManager> s_sp = ams::sf::GetSharedPointerTo<gpio::sf::IManager>(g_manager_impl);
return s_sp;
}
}
std::shared_ptr<gpio::sf::IManager> GetServiceObject() {
return GetManagerServiceObject();
}
}

View File

@ -0,0 +1,86 @@
/*
* 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>
#include "gpio_server_manager_impl.hpp"
namespace ams::gpio::server {
ManagerImpl::ManagerImpl() : pad_session_memory_resource(), pad_allocator(std::addressof(pad_session_memory_resource)) {
this->heap_handle = lmem::CreateExpHeap(this->heap_buffer, sizeof(this->heap_buffer), lmem::CreateOption_None);
this->pad_session_memory_resource.Attach(this->heap_handle);
}
ManagerImpl::~ManagerImpl() {
lmem::DestroyExpHeap(this->heap_handle);
}
Result ManagerImpl::OpenSessionForDev(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, s32 pad_descriptor) {
/* TODO */
AMS_ABORT();
}
Result ManagerImpl::OpenSession(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, gpio::GpioPadName pad_name) {
/* TODO */
AMS_ABORT();
}
Result ManagerImpl::OpenSessionForTest(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, gpio::GpioPadName pad_name) {
/* TODO */
AMS_ABORT();
}
Result ManagerImpl::IsWakeEventActive(ams::sf::Out<bool> out, gpio::GpioPadName pad_name) {
/* TODO */
AMS_ABORT();
}
Result ManagerImpl::GetWakeEventActiveFlagSet(ams::sf::Out<gpio::WakeBitFlag> out) {
/* TODO */
AMS_ABORT();
}
Result ManagerImpl::SetWakeEventActiveFlagSetForDebug(gpio::GpioPadName pad_name, bool is_enabled) {
/* TODO */
AMS_ABORT();
}
Result ManagerImpl::SetWakePinDebugMode(s32 mode) {
/* TODO */
AMS_ABORT();
}
Result ManagerImpl::OpenSession2(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, DeviceCode device_code, ddsf::AccessMode access_mode) {
/* TODO */
AMS_ABORT();
}
Result ManagerImpl::IsWakeEventActive2(ams::sf::Out<bool> out, DeviceCode device_code) {
/* TODO */
AMS_ABORT();
}
Result ManagerImpl::SetWakeEventActiveFlagSetForDebug2(DeviceCode device_code, bool is_enabled) {
/* TODO */
AMS_ABORT();
}
Result ManagerImpl::SetRetryValues(u32 arg0, u32 arg1) {
/* TODO */
AMS_ABORT();
}
}

View File

@ -0,0 +1,49 @@
/*
* 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.hpp>
#include "gpio_server_pad_session_impl.hpp"
namespace ams::gpio::server {
class ManagerImpl {
private:
lmem::HeapHandle heap_handle;
ams::sf::ExpHeapMemoryResource pad_session_memory_resource;
typename ams::sf::ServiceObjectAllocator<gpio::sf::IPadSession, PadSessionImpl> pad_allocator;
u8 heap_buffer[12_KB];
public:
ManagerImpl();
~ManagerImpl();
public:
/* Actual commands. */
Result OpenSessionForDev(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, s32 pad_descriptor);
Result OpenSession(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, gpio::GpioPadName pad_name);
Result OpenSessionForTest(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, gpio::GpioPadName pad_name);
Result IsWakeEventActive(ams::sf::Out<bool> out, gpio::GpioPadName pad_name);
Result GetWakeEventActiveFlagSet(ams::sf::Out<gpio::WakeBitFlag> out);
Result SetWakeEventActiveFlagSetForDebug(gpio::GpioPadName pad_name, bool is_enabled);
Result SetWakePinDebugMode(s32 mode);
Result OpenSession2(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, DeviceCode device_code, ddsf::AccessMode access_mode);
Result IsWakeEventActive2(ams::sf::Out<bool> out, DeviceCode device_code);
Result SetWakeEventActiveFlagSetForDebug2(DeviceCode device_code, bool is_enabled);
Result SetRetryValues(u32 arg0, u32 arg1);
};
static_assert(gpio::sf::IsIManager<ManagerImpl>);
}

View File

@ -0,0 +1,207 @@
/*
* 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.hpp>
namespace ams::gpio::server {
class ManagerImpl;
class PadSessionImpl {
private:
ManagerImpl *parent; /* NOTE: this is an sf::SharedPointer<> in Nintendo's code. */
gpio::driver::GpioPadSession internal_pad_session;
bool has_session;
os::SystemEvent system_event;
public:
explicit PadSessionImpl(ManagerImpl *p) : parent(p), has_session(false) { /* ... */ }
~PadSessionImpl() {
if (this->has_session) {
gpio::driver::CloseSession(std::addressof(this->internal_pad_session));
}
}
Result OpenSession(DeviceCode device_code) {
AMS_ABORT_UNLESS(!this->has_session);
R_TRY(gpio::driver::OpenSession(std::addressof(this->internal_pad_session), device_code));
this->has_session = true;
return ResultSuccess();
}
public:
/* Actual commands. */
Result SetDirection(gpio::Direction direction) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* Validate the direction. */
R_UNLESS((direction == Direction_Input || direction == Direction_Output), gpio::ResultInvalidArgument());
/* Invoke the driver library. */
R_TRY(gpio::driver::SetDirection(std::addressof(this->internal_pad_session), direction));
return ResultSuccess();
}
Result GetDirection(ams::sf::Out<gpio::Direction> out) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* Invoke the driver library. */
R_TRY(gpio::driver::GetDirection(out.GetPointer(), std::addressof(this->internal_pad_session)));
return ResultSuccess();
}
Result SetInterruptMode(gpio::InterruptMode mode) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* TODO */
AMS_ABORT();
}
Result GetInterruptMode(ams::sf::Out<gpio::InterruptMode> out) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* TODO */
AMS_ABORT();
}
Result SetInterruptEnable(bool enable) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* TODO */
AMS_ABORT();
}
Result GetInterruptEnable(ams::sf::Out<bool> out) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* TODO */
AMS_ABORT();
}
Result GetInterruptStatus(ams::sf::Out<gpio::InterruptStatus> out) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* TODO */
AMS_ABORT();
}
Result ClearInterruptStatus() {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* TODO */
AMS_ABORT();
}
Result SetValue(gpio::GpioValue value) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* Validate the value. */
R_UNLESS((value == GpioValue_Low || value == GpioValue_High), gpio::ResultInvalidArgument());
/* Invoke the driver library. */
R_TRY(gpio::driver::SetValue(std::addressof(this->internal_pad_session), value));
return ResultSuccess();
}
Result GetValue(ams::sf::Out<gpio::GpioValue> out) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* Invoke the driver library. */
R_TRY(gpio::driver::GetValue(out.GetPointer(), std::addressof(this->internal_pad_session)));
return ResultSuccess();
}
Result BindInterrupt(ams::sf::OutCopyHandle out) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* TODO */
AMS_ABORT();
}
Result UnbindInterrupt() {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* TODO */
AMS_ABORT();
}
Result SetDebounceEnabled(bool enable) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* TODO */
AMS_ABORT();
}
Result GetDebounceEnabled(ams::sf::Out<bool> out) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* TODO */
AMS_ABORT();
}
Result SetDebounceTime(s32 ms) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* TODO */
AMS_ABORT();
}
Result GetDebounceTime(ams::sf::Out<s32> out) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* TODO */
AMS_ABORT();
}
Result SetValueForSleepState(gpio::GpioValue value) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* TODO */
AMS_ABORT();
}
Result GetValueForSleepState(ams::sf::Out<gpio::GpioValue> out) {
/* Validate our state. */
AMS_ASSERT(this->has_session);
/* TODO */
AMS_ABORT();
}
};
static_assert(gpio::sf::IsIPadSession<PadSessionImpl>);
}

View File

@ -15,6 +15,7 @@
*/
#include <stratosphere.hpp>
#include "impl/os_interrupt_event_impl.hpp"
#include "impl/os_waitable_holder_impl.hpp"
#include "impl/os_waitable_object_list.hpp"
namespace ams::os {
@ -61,4 +62,12 @@ namespace ams::os {
return GetReference(event->impl).Clear();
}
void InitializeWaitableHolder(WaitableHolderType *waitable_holder, InterruptEventType *event) {
AMS_ASSERT(event->state == InterruptEventType::State_Initialized);
new (GetPointer(waitable_holder->impl_storage)) impl::WaitableHolderOfInterruptEvent(event);
waitable_holder->user_data = 0;
}
}

View File

@ -33,6 +33,7 @@
#include <vapours/results/err_results.hpp>
#include <vapours/results/fatal_results.hpp>
#include <vapours/results/fs_results.hpp>
#include <vapours/results/gpio_results.hpp>
#include <vapours/results/hipc_results.hpp>
#include <vapours/results/i2c_results.hpp>
#include <vapours/results/kvdb_results.hpp>

View File

@ -0,0 +1,29 @@
/*
* 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/results/results_common.hpp>
namespace ams::gpio {
R_DEFINE_NAMESPACE_RESULT_MODULE(102);
R_DEFINE_ERROR_RESULT(AlreadyBound, 1);
R_DEFINE_ERROR_RESULT(AlreadyOpen, 2);
R_DEFINE_ERROR_RESULT(DeviceNotFound, 3);
R_DEFINE_ERROR_RESULT(InvalidArgument, 4);
}

View File

@ -0,0 +1,32 @@
/*
* 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>
#include "boot_driver_management.hpp"
namespace ams::boot {
void InitializeGpioDriverLibrary() {
/* Initialize the gpio client library with the server manager object. */
gpio::InitializeWith(gpio::server::GetServiceObject());
/* Initialize the board driver without enabling interrupt handlers. */
gpio::driver::board::Initialize(false);
/* Initialize the driver library. */
gpio::driver::Initialize();
}
}

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
namespace ams::boot {
void InitializeGpioDriverLibrary();
}

View File

@ -19,6 +19,7 @@
#include "boot_check_battery.hpp"
#include "boot_check_clock.hpp"
#include "boot_clock_initial_configuration.hpp"
#include "boot_driver_management.hpp"
#include "boot_fan_enable.hpp"
#include "boot_repair_boot_images.hpp"
#include "boot_splash_screen.hpp"
@ -71,13 +72,13 @@ using namespace ams;
namespace {
u8 g_exp_heap_memory[20_KB];
u8 g_unit_heap_memory[5_KB];
lmem::HeapHandle g_exp_heap_handle;
lmem::HeapHandle g_unit_heap_handle;
constinit u8 g_exp_heap_memory[20_KB];
constinit u8 g_unit_heap_memory[5_KB];
constinit lmem::HeapHandle g_exp_heap_handle;
constinit lmem::HeapHandle g_unit_heap_handle;
std::optional<sf::ExpHeapMemoryResource> g_exp_heap_memory_resource;
std::optional<sf::UnitHeapMemoryResource> g_unit_heap_memory_resource;
constinit sf::ExpHeapMemoryResource g_exp_heap_memory_resource;
constinit sf::UnitHeapMemoryResource g_unit_heap_memory_resource;
void *Allocate(size_t size) {
void *mem = lmem::AllocateFromExpHeap(g_exp_heap_handle, size);
@ -94,13 +95,13 @@ namespace {
g_exp_heap_handle = lmem::CreateExpHeap(g_exp_heap_memory, sizeof(g_exp_heap_memory), lmem::CreateOption_ThreadSafe);
g_unit_heap_handle = lmem::CreateUnitHeap(g_unit_heap_memory, sizeof(g_unit_heap_memory), sizeof(ddsf::DeviceCodeEntryHolder), lmem::CreateOption_ThreadSafe);
/* Create the memory resources. */
g_exp_heap_memory_resource.emplace(g_exp_heap_handle);
g_unit_heap_memory_resource.emplace(g_unit_heap_handle);
/* Attach the memory resources. */
g_exp_heap_memory_resource.Attach(g_exp_heap_handle);
g_unit_heap_memory_resource.Attach(g_unit_heap_handle);
/* Register with ddsf. */
ddsf::SetMemoryResource(std::addressof(*g_exp_heap_memory_resource));
ddsf::SetDeviceCodeEntryHolderMemoryResource(std::addressof(*g_unit_heap_memory_resource));
ddsf::SetMemoryResource(std::addressof(g_exp_heap_memory_resource));
ddsf::SetDeviceCodeEntryHolderMemoryResource(std::addressof(g_unit_heap_memory_resource));
}
}
@ -184,9 +185,12 @@ int main(int argc, char **argv)
/* Change voltage from 3.3v to 1.8v for select devices. */
boot::ChangeGpioVoltageTo1_8v();
/* Setup GPIO. */
/* Setup gpio. */
gpio::driver::SetInitialGpioConfig();
/* Initialize the gpio server library. */
boot::InitializeGpioDriverLibrary();
/* Check USB PLL/UTMIP clock. */
boot::CheckClock();