pwm: implement driver for boot sysmodule

This commit is contained in:
Michael Scire 2020-11-01 23:04:19 -08:00 committed by SciresM
parent 35552bac2c
commit 3d31837ca1
41 changed files with 2107 additions and 59 deletions

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 "pwm_server_manager_impl.hpp"
namespace ams::pwm::server {
namespace {
ManagerImpl g_manager_impl;
std::shared_ptr<pwm::sf::IManager> GetManagerServiceObject() {
static std::shared_ptr<pwm::sf::IManager> s_sp = ams::sf::GetSharedPointerTo<pwm::sf::IManager>(g_manager_impl);
return s_sp;
}
}
std::shared_ptr<pwm::sf::IManager> GetServiceObject() {
return GetManagerServiceObject();
}
}

View file

@ -0,0 +1,88 @@
/*
* 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::pwm::server {
class ManagerImpl;
class ChannelSessionImpl {
private:
ManagerImpl *parent; /* NOTE: this is an sf::SharedPointer<> in Nintendo's code. */
pwm::driver::ChannelSession internal_session;
bool has_session;
public:
explicit ChannelSessionImpl(ManagerImpl *p) : parent(p), has_session(false) { /* ... */ }
~ChannelSessionImpl() {
if (this->has_session) {
pwm::driver::CloseSession(this->internal_session);
}
}
Result OpenSession(DeviceCode device_code) {
AMS_ABORT_UNLESS(!this->has_session);
R_TRY(pwm::driver::OpenSession(std::addressof(this->internal_session), device_code));
this->has_session = true;
return ResultSuccess();
}
public:
/* Actual commands. */
Result SetPeriod(TimeSpanType period) {
pwm::driver::SetPeriod(this->internal_session, period);
return ResultSuccess();
}
Result GetPeriod(ams::sf::Out<TimeSpanType> out) {
out.SetValue(pwm::driver::GetPeriod(this->internal_session));
return ResultSuccess();
}
Result SetDuty(int duty) {
pwm::driver::SetDuty(this->internal_session, duty);
return ResultSuccess();
}
Result GetDuty(ams::sf::Out<int> out) {
out.SetValue(pwm::driver::GetDuty(this->internal_session));
return ResultSuccess();
}
Result SetEnabled(bool enabled) {
pwm::driver::SetEnabled(this->internal_session, enabled);
return ResultSuccess();
}
Result GetEnabled(ams::sf::Out<bool> out) {
out.SetValue(pwm::driver::GetEnabled(this->internal_session));
return ResultSuccess();
}
Result SetScale(double scale) {
pwm::driver::SetScale(this->internal_session, scale);
return ResultSuccess();
}
Result GetScale(ams::sf::Out<double> out) {
out.SetValue(pwm::driver::GetScale(this->internal_session));
return ResultSuccess();
}
};
static_assert(pwm::sf::IsIChannelSession<ChannelSessionImpl>);
}

View file

@ -0,0 +1,51 @@
/*
* 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 "pwm_server_manager_impl.hpp"
namespace ams::pwm::server {
ManagerImpl::ManagerImpl() : session_memory_resource(), allocator(std::addressof(session_memory_resource)) {
this->heap_handle = lmem::CreateExpHeap(this->heap_buffer, sizeof(this->heap_buffer), lmem::CreateOption_None);
this->session_memory_resource.Attach(this->heap_handle);
}
ManagerImpl::~ManagerImpl() {
lmem::DestroyExpHeap(this->heap_handle);
}
Result ManagerImpl::OpenSessionForDev(ams::sf::Out<std::shared_ptr<pwm::sf::IChannelSession>> out, int channel) {
/* TODO */
AMS_ABORT();
}
Result ManagerImpl::OpenSession(ams::sf::Out<std::shared_ptr<pwm::sf::IChannelSession>> out, pwm::ChannelName channel_name) {
return this->OpenSession2(out, ConvertToDeviceCode(channel_name));
}
Result ManagerImpl::OpenSession2(ams::sf::Out<std::shared_ptr<pwm::sf::IChannelSession>> out, DeviceCode device_code) {
/* Allocate a session. */
auto session = ams::sf::AllocateShared<pwm::sf::IChannelSession, ChannelSessionImpl>(this->allocator, this);
/* Open the session. */
R_TRY(session->GetImpl().OpenSession(device_code));
/* We succeeded. */
out.SetValue(std::move(session));
return ResultSuccess();
}
}

View file

@ -0,0 +1,40 @@
/*
* 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 "pwm_server_channel_session_impl.hpp"
namespace ams::pwm::server {
class ManagerImpl {
private:
lmem::HeapHandle heap_handle;
ams::sf::ExpHeapMemoryResource session_memory_resource;
typename ams::sf::ServiceObjectAllocator<pwm::sf::IChannelSession, ChannelSessionImpl> allocator;
u8 heap_buffer[4_KB];
public:
ManagerImpl();
~ManagerImpl();
public:
/* Actual commands. */
Result OpenSessionForDev(ams::sf::Out<std::shared_ptr<pwm::sf::IChannelSession>> out, int channel);
Result OpenSession(ams::sf::Out<std::shared_ptr<pwm::sf::IChannelSession>> out, pwm::ChannelName channel_name);
Result OpenSession2(ams::sf::Out<std::shared_ptr<pwm::sf::IChannelSession>> out, DeviceCode device_code);
};
static_assert(pwm::sf::IsIManager<ManagerImpl>);
}