From 35552bac2c5aa53c31836ed91846fb6f58da9b70 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sun, 1 Nov 2020 23:04:01 -0800 Subject: [PATCH] gpio: add missing driver functions for boot --- .../source/gpio/driver/gpio_pad_api.cpp | 137 ++++++++++++++++++ .../include/vapours/results/gpio_results.hpp | 2 + 2 files changed, 139 insertions(+) create mode 100644 libraries/libstratosphere/source/gpio/driver/gpio_pad_api.cpp diff --git a/libraries/libstratosphere/source/gpio/driver/gpio_pad_api.cpp b/libraries/libstratosphere/source/gpio/driver/gpio_pad_api.cpp new file mode 100644 index 000000000..b2aaaed84 --- /dev/null +++ b/libraries/libstratosphere/source/gpio/driver/gpio_pad_api.cpp @@ -0,0 +1,137 @@ +/* + * 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 . + */ +#include + +#include "impl/gpio_driver_core.hpp" + +namespace ams::gpio::driver { + + namespace { + + Result OpenSessionImpl(GpioPadSession *out, Pad *pad, ddsf::AccessMode access_mode) { + /* Construct the session. */ + auto *session = new (std::addressof(impl::GetPadSessionImpl(*out))) impl::PadSessionImpl; + auto session_guard = SCOPE_GUARD { session->~PadSessionImpl(); }; + + /* Open the session. */ + R_TRY(session->Open(pad, access_mode)); + + session_guard.Cancel(); + return ResultSuccess(); + } + + } + + Result OpenSession(GpioPadSession *out, DeviceCode device_code, ddsf::AccessMode access_mode) { + /* Find the pad. */ + Pad *pad = nullptr; + R_TRY(impl::FindPad(std::addressof(pad), device_code)); + AMS_ASSERT(pad != nullptr); + + /* Sanitize the access mode. */ + if ((access_mode & ~(ddsf::AccessMode_ReadWrite)) != 0) { + access_mode = ddsf::AccessMode_None; + } + + /* Try to open the session with the desired mode. */ + R_TRY_CATCH(OpenSessionImpl(out, pad, access_mode)) { + R_CATCH(ddsf::ResultAccessModeDenied) { + /* Our current access mode was denied. Try adding the shared flag. */ + R_TRY(OpenSessionImpl(out, pad, static_cast(access_mode | ddsf::AccessMode_Shared))); + } + } R_END_TRY_CATCH; + + return ResultSuccess(); + } + + void CloseSession(GpioPadSession *session) { + AMS_ASSERT(session != nullptr); + impl::GetOpenPadSessionImpl(*session).~PadSessionImpl(); + } + + Result SetDirection(GpioPadSession *session, gpio::Direction direction) { + /* Check that we have a valid session. */ + AMS_ASSERT(session != nullptr); + auto &impl = impl::GetOpenPadSessionImpl(*session); + R_UNLESS(impl.IsOpen(), gpio::ResultNotOpen()); + + /* Check that we can perform the access. */ + R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied()); + + /* Get the pad. */ + auto &pad = impl.GetDevice().SafeCastTo(); + + /* Perform the call. */ + R_TRY(pad.GetDriver().SafeCastTo().SetDirection(std::addressof(pad), direction)); + + return ResultSuccess(); + } + + Result GetDirection(gpio::Direction *out, GpioPadSession *session) { + /* Check that we have a valid session. */ + AMS_ASSERT(session != nullptr); + auto &impl = impl::GetOpenPadSessionImpl(*session); + R_UNLESS(impl.IsOpen(), gpio::ResultNotOpen()); + + /* Check that we can perform the access. */ + R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied()); + + /* Get the pad. */ + auto &pad = impl.GetDevice().SafeCastTo(); + + /* Perform the call. */ + R_TRY(pad.GetDriver().SafeCastTo().GetDirection(out, std::addressof(pad))); + + return ResultSuccess(); + } + + Result SetValue(GpioPadSession *session, gpio::GpioValue value) { + /* Check that we have a valid session. */ + AMS_ASSERT(session != nullptr); + auto &impl = impl::GetOpenPadSessionImpl(*session); + R_UNLESS(impl.IsOpen(), gpio::ResultNotOpen()); + + /* Check that we can perform the access. */ + R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Write), ddsf::ResultPermissionDenied()); + + /* Get the pad. */ + auto &pad = impl.GetDevice().SafeCastTo(); + + /* Perform the call. */ + R_TRY(pad.GetDriver().SafeCastTo().SetValue(std::addressof(pad), value)); + + return ResultSuccess(); + } + + Result GetValue(gpio::GpioValue *out, GpioPadSession *session) { + /* Check that we have a valid session. */ + AMS_ASSERT(session != nullptr); + auto &impl = impl::GetOpenPadSessionImpl(*session); + R_UNLESS(impl.IsOpen(), gpio::ResultNotOpen()); + + /* Check that we can perform the access. */ + R_UNLESS(impl.CheckAccess(ddsf::AccessMode_Read), ddsf::ResultPermissionDenied()); + + /* Get the pad. */ + auto &pad = impl.GetDevice().SafeCastTo(); + + /* Perform the call. */ + R_TRY(pad.GetDriver().SafeCastTo().GetValue(out, std::addressof(pad))); + + return ResultSuccess(); + } + +} diff --git a/libraries/libvapours/include/vapours/results/gpio_results.hpp b/libraries/libvapours/include/vapours/results/gpio_results.hpp index 4f4cc6a5f..546a602e7 100644 --- a/libraries/libvapours/include/vapours/results/gpio_results.hpp +++ b/libraries/libvapours/include/vapours/results/gpio_results.hpp @@ -26,4 +26,6 @@ namespace ams::gpio { R_DEFINE_ERROR_RESULT(DeviceNotFound, 3); R_DEFINE_ERROR_RESULT(InvalidArgument, 4); + R_DEFINE_ERROR_RESULT(NotOpen, 6); + }