mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-05-28 13:44:11 -04:00
i2c/gpio: hook up open session for sf interface
This commit is contained in:
parent
b74b309a77
commit
42caa4ffd1
15 changed files with 571 additions and 16 deletions
|
@ -33,8 +33,7 @@ namespace ams::gpio::server {
|
|||
}
|
||||
|
||||
Result ManagerImpl::OpenSession(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, gpio::GpioPadName pad_name) {
|
||||
/* TODO */
|
||||
AMS_ABORT();
|
||||
return this->OpenSession2(out, ConvertToDeviceCode(pad_name), ddsf::AccessMode_ReadWrite);
|
||||
}
|
||||
|
||||
Result ManagerImpl::OpenSessionForTest(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, gpio::GpioPadName pad_name) {
|
||||
|
@ -63,8 +62,15 @@ namespace ams::gpio::server {
|
|||
}
|
||||
|
||||
Result ManagerImpl::OpenSession2(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, DeviceCode device_code, ddsf::AccessMode access_mode) {
|
||||
/* TODO */
|
||||
AMS_ABORT();
|
||||
/* Allocate a session. */
|
||||
auto session = ams::sf::AllocateShared<gpio::sf::IPadSession, PadSessionImpl>(this->pad_allocator, this);
|
||||
|
||||
/* Open the session. */
|
||||
R_TRY(session->GetImpl().OpenSession(device_code, access_mode));
|
||||
|
||||
/* We succeeded. */
|
||||
out.SetValue(std::move(session));
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result ManagerImpl::IsWakeEventActive2(ams::sf::Out<bool> out, DeviceCode device_code) {
|
||||
|
@ -82,5 +88,4 @@ namespace ams::gpio::server {
|
|||
AMS_ABORT();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -35,10 +35,10 @@ namespace ams::gpio::server {
|
|||
}
|
||||
}
|
||||
|
||||
Result OpenSession(DeviceCode device_code) {
|
||||
Result OpenSession(DeviceCode device_code, ddsf::AccessMode access_mode) {
|
||||
AMS_ABORT_UNLESS(!this->has_session);
|
||||
|
||||
R_TRY(gpio::driver::OpenSession(std::addressof(this->internal_pad_session), device_code));
|
||||
R_TRY(gpio::driver::OpenSession(std::addressof(this->internal_pad_session), device_code, access_mode));
|
||||
this->has_session = true;
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
|
|
@ -30,6 +30,21 @@ namespace ams::i2c {
|
|||
std::shared_ptr<sf::IManager> g_i2c_pcv_manager;
|
||||
constinit int g_i2c_pcv_count = 0;
|
||||
|
||||
using InternalSession = std::shared_ptr<i2c::sf::ISession>;
|
||||
|
||||
InternalSession &GetInterface(const I2cSession &session) {
|
||||
AMS_ASSERT(session._session != nullptr);
|
||||
return *static_cast<InternalSession *>(session._session);
|
||||
}
|
||||
|
||||
std::shared_ptr<sf::IManager> GetManager(DeviceCode device_code) {
|
||||
if (IsPowerBusDeviceCode(device_code)) {
|
||||
return g_i2c_pcv_manager;
|
||||
} else {
|
||||
return g_i2c_manager;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void InitializeWith(std::shared_ptr<i2c::sf::IManager> &&sp, std::shared_ptr<i2c::sf::IManager> &&sp_pcv) {
|
||||
|
@ -83,4 +98,64 @@ namespace ams::i2c {
|
|||
}
|
||||
}
|
||||
|
||||
Result OpenSession(I2cSession *out, DeviceCode device_code) {
|
||||
/* Allocate the session. */
|
||||
InternalSession *internal_session = new (std::nothrow) InternalSession;
|
||||
AMS_ABORT_UNLESS(internal_session != nullptr);
|
||||
auto session_guard = SCOPE_GUARD { delete internal_session; };
|
||||
|
||||
/* Get manager for the device. */
|
||||
auto manager = GetManager(device_code);
|
||||
|
||||
/* Get the session. */
|
||||
{
|
||||
ams::sf::cmif::ServiceObjectHolder object_holder;
|
||||
if (hos::GetVersion() >= hos::Version_6_0_0) {
|
||||
R_TRY(manager->OpenSession2(std::addressof(object_holder), device_code));
|
||||
} else {
|
||||
R_TRY(manager->OpenSession(std::addressof(object_holder), ConvertToI2cDevice(device_code)));
|
||||
}
|
||||
*internal_session = object_holder.GetServiceObject<sf::ISession>();
|
||||
}
|
||||
|
||||
/* Set output. */
|
||||
out->_session = internal_session;
|
||||
|
||||
/* We succeeded. */
|
||||
session_guard.Cancel();
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
void CloseSession(I2cSession &session) {
|
||||
/* Close the session. */
|
||||
delete std::addressof(GetInterface(session));
|
||||
session._session = nullptr;
|
||||
}
|
||||
|
||||
Result Send(const I2cSession &session, const void *src, size_t src_size, TransactionOption option) {
|
||||
const ams::sf::InAutoSelectBuffer buf(src, src_size);
|
||||
|
||||
return GetInterface(session)->Send(buf, option);
|
||||
}
|
||||
|
||||
Result Receive(void *dst, size_t dst_size, const I2cSession &session, TransactionOption option) {
|
||||
const ams::sf::OutAutoSelectBuffer buf(dst, dst_size);
|
||||
|
||||
return GetInterface(session)->Receive(buf, option);
|
||||
}
|
||||
|
||||
Result ExecuteCommandList(void *dst, size_t dst_size, const I2cSession &session, const void *src, size_t src_size) {
|
||||
const ams::sf::OutAutoSelectBuffer buf(dst, dst_size);
|
||||
const ams::sf::InPointerArray<i2c::I2cCommand> arr(static_cast<const i2c::I2cCommand *>(src), src_size);
|
||||
|
||||
return GetInterface(session)->ExecuteCommandList(buf, arr);
|
||||
}
|
||||
|
||||
void SetRetryPolicy(const I2cSession &session, int max_retry_count, int retry_interval_ms) {
|
||||
AMS_ASSERT(max_retry_count >= 0);
|
||||
AMS_ASSERT(retry_interval_ms >= 0);
|
||||
|
||||
R_ABORT_UNLESS(GetInterface(session)->SetRetryPolicy(max_retry_count, retry_interval_ms));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* 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 "i2c_server_manager_impl.hpp"
|
||||
|
||||
namespace ams::i2c::server {
|
||||
|
||||
namespace {
|
||||
|
||||
ManagerImpl g_manager_impl;
|
||||
ManagerImpl g_pcv_manager_impl;
|
||||
|
||||
std::shared_ptr<i2c::sf::IManager> GetManagerServiceObject() {
|
||||
static std::shared_ptr<i2c::sf::IManager> s_sp = ams::sf::GetSharedPointerTo<i2c::sf::IManager>(g_manager_impl);
|
||||
return s_sp;
|
||||
}
|
||||
|
||||
std::shared_ptr<i2c::sf::IManager> GetManagerServiceObjectPowerBus() {
|
||||
static std::shared_ptr<i2c::sf::IManager> s_sp = ams::sf::GetSharedPointerTo<i2c::sf::IManager>(g_pcv_manager_impl);
|
||||
return s_sp;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::shared_ptr<i2c::sf::IManager> GetServiceObject() {
|
||||
return GetManagerServiceObject();
|
||||
}
|
||||
|
||||
std::shared_ptr<i2c::sf::IManager> GetServiceObjectPowerBus() {
|
||||
return GetManagerServiceObjectPowerBus();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* 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 "i2c_server_manager_impl.hpp"
|
||||
|
||||
namespace ams::i2c::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<i2c::sf::ISession>> out, s32 bus_idx, u16 slave_address, i2c::AddressingMode addressing_mode, i2c::SpeedMode speed_mode) {
|
||||
/* TODO */
|
||||
AMS_ABORT();
|
||||
}
|
||||
|
||||
Result ManagerImpl::OpenSession(ams::sf::Out<std::shared_ptr<i2c::sf::ISession>> out, i2c::I2cDevice device) {
|
||||
return this->OpenSession2(out, ConvertToDeviceCode(device));
|
||||
}
|
||||
|
||||
Result ManagerImpl::HasDevice(ams::sf::Out<bool> out, i2c::I2cDevice device) {
|
||||
/* TODO */
|
||||
AMS_ABORT();
|
||||
}
|
||||
|
||||
Result ManagerImpl::HasDeviceForDev(ams::sf::Out<bool> out, i2c::I2cDevice device) {
|
||||
/* TODO */
|
||||
AMS_ABORT();
|
||||
}
|
||||
|
||||
Result ManagerImpl::OpenSession2(ams::sf::Out<std::shared_ptr<i2c::sf::ISession>> out, DeviceCode device_code) {
|
||||
/* Allocate a session. */
|
||||
auto session = ams::sf::AllocateShared<i2c::sf::ISession, SessionImpl>(this->allocator, this);
|
||||
|
||||
/* Open the session. */
|
||||
R_TRY(session->GetImpl().OpenSession(device_code));
|
||||
|
||||
/* We succeeded. */
|
||||
out.SetValue(std::move(session));
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* 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 "i2c_server_session_impl.hpp"
|
||||
|
||||
namespace ams::i2c::server {
|
||||
|
||||
class ManagerImpl {
|
||||
private:
|
||||
lmem::HeapHandle heap_handle;
|
||||
ams::sf::ExpHeapMemoryResource session_memory_resource;
|
||||
typename ams::sf::ServiceObjectAllocator<i2c::sf::ISession, SessionImpl> allocator;
|
||||
u8 heap_buffer[4_KB];
|
||||
public:
|
||||
ManagerImpl();
|
||||
|
||||
~ManagerImpl();
|
||||
public:
|
||||
/* Actual commands. */
|
||||
Result OpenSessionForDev(ams::sf::Out<std::shared_ptr<i2c::sf::ISession>> out, s32 bus_idx, u16 slave_address, i2c::AddressingMode addressing_mode, i2c::SpeedMode speed_mode);
|
||||
Result OpenSession(ams::sf::Out<std::shared_ptr<i2c::sf::ISession>> out, i2c::I2cDevice device);
|
||||
Result HasDevice(ams::sf::Out<bool> out, i2c::I2cDevice device);
|
||||
Result HasDeviceForDev(ams::sf::Out<bool> out, i2c::I2cDevice device);
|
||||
Result OpenSession2(ams::sf::Out<std::shared_ptr<i2c::sf::ISession>> out, DeviceCode device_code);
|
||||
};
|
||||
static_assert(i2c::sf::IsIManager<ManagerImpl>);
|
||||
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* 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::i2c::server {
|
||||
|
||||
class ManagerImpl;
|
||||
|
||||
class SessionImpl {
|
||||
private:
|
||||
ManagerImpl *parent; /* NOTE: this is an sf::SharedPointer<> in Nintendo's code. */
|
||||
i2c::driver::I2cSession internal_session;
|
||||
bool has_session;
|
||||
public:
|
||||
explicit SessionImpl(ManagerImpl *p) : parent(p), has_session(false) { /* ... */ }
|
||||
|
||||
~SessionImpl() {
|
||||
if (this->has_session) {
|
||||
i2c::driver::CloseSession(this->internal_session);
|
||||
}
|
||||
}
|
||||
|
||||
Result OpenSession(DeviceCode device_code) {
|
||||
AMS_ABORT_UNLESS(!this->has_session);
|
||||
|
||||
R_TRY(i2c::driver::OpenSession(std::addressof(this->internal_session), device_code));
|
||||
this->has_session = true;
|
||||
return ResultSuccess();
|
||||
}
|
||||
public:
|
||||
/* Actual commands. */
|
||||
Result SendOld(const ams::sf::InBuffer &in_data, i2c::TransactionOption option) {
|
||||
return i2c::driver::Send(this->internal_session, in_data.GetPointer(), in_data.GetSize(), option);
|
||||
}
|
||||
|
||||
Result ReceiveOld(const ams::sf::OutBuffer &out_data, i2c::TransactionOption option) {
|
||||
return i2c::driver::Receive(out_data.GetPointer(), out_data.GetSize(), this->internal_session, option);
|
||||
}
|
||||
|
||||
Result ExecuteCommandListOld(const ams::sf::OutBuffer &rcv_buf, const ams::sf::InPointerArray<i2c::I2cCommand> &command_list){
|
||||
return i2c::driver::ExecuteCommandList(rcv_buf.GetPointer(), rcv_buf.GetSize(), this->internal_session, command_list.GetPointer(), command_list.GetSize() * sizeof(i2c::I2cCommand));
|
||||
}
|
||||
|
||||
Result Send(const ams::sf::InAutoSelectBuffer &in_data, i2c::TransactionOption option) {
|
||||
return i2c::driver::Send(this->internal_session, in_data.GetPointer(), in_data.GetSize(), option);
|
||||
}
|
||||
|
||||
Result Receive(const ams::sf::OutAutoSelectBuffer &out_data, i2c::TransactionOption option) {
|
||||
return i2c::driver::Receive(out_data.GetPointer(), out_data.GetSize(), this->internal_session, option);
|
||||
}
|
||||
|
||||
Result ExecuteCommandList(const ams::sf::OutAutoSelectBuffer &rcv_buf, const ams::sf::InPointerArray<i2c::I2cCommand> &command_list) {
|
||||
return i2c::driver::ExecuteCommandList(rcv_buf.GetPointer(), rcv_buf.GetSize(), this->internal_session, command_list.GetPointer(), command_list.GetSize() * sizeof(i2c::I2cCommand));
|
||||
}
|
||||
|
||||
Result SetRetryPolicy(s32 max_retry_count, s32 retry_interval_ms) {
|
||||
return i2c::driver::SetRetryPolicy(this->internal_session, max_retry_count, retry_interval_ms);
|
||||
}
|
||||
};
|
||||
static_assert(i2c::sf::IsISession<SessionImpl>);
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue