2020-10-31 12:55:11 +01:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-10-31 12:55:11 +01:00
|
|
|
*
|
|
|
|
* 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::i2c {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constinit os::SdkMutex g_init_mutex;
|
|
|
|
constinit int g_initialize_count = 0;
|
|
|
|
|
|
|
|
constinit os::SdkMutex g_i2c_mutex;
|
2021-01-17 16:55:32 +01:00
|
|
|
ams::sf::SharedPointer<sf::IManager> g_i2c_manager;
|
2020-10-31 12:55:11 +01:00
|
|
|
constinit int g_i2c_count = 0;
|
|
|
|
|
|
|
|
constinit os::SdkMutex g_i2c_pcv_mutex;
|
2021-01-17 16:55:32 +01:00
|
|
|
ams::sf::SharedPointer<sf::IManager> g_i2c_pcv_manager;
|
2020-10-31 12:55:11 +01:00
|
|
|
constinit int g_i2c_pcv_count = 0;
|
|
|
|
|
2021-01-17 16:55:32 +01:00
|
|
|
i2c::sf::ISession *GetInterface(const I2cSession &session) {
|
2020-11-01 05:50:21 +01:00
|
|
|
AMS_ASSERT(session._session != nullptr);
|
2021-01-17 16:55:32 +01:00
|
|
|
return static_cast<i2c::sf::ISession *>(session._session);
|
2020-11-01 05:50:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-17 16:55:32 +01:00
|
|
|
ams::sf::SharedPointer<sf::IManager> GetManager(DeviceCode device_code) {
|
2020-11-01 05:50:21 +01:00
|
|
|
if (IsPowerBusDeviceCode(device_code)) {
|
|
|
|
return g_i2c_pcv_manager;
|
|
|
|
} else {
|
|
|
|
return g_i2c_manager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 12:55:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-17 16:55:32 +01:00
|
|
|
void InitializeWith(ams::sf::SharedPointer<i2c::sf::IManager> sp, ams::sf::SharedPointer<i2c::sf::IManager> sp_pcv) {
|
2020-10-31 12:55:11 +01:00
|
|
|
std::scoped_lock lk(g_init_mutex);
|
|
|
|
|
|
|
|
AMS_ABORT_UNLESS(g_initialize_count == 0);
|
|
|
|
|
|
|
|
{
|
|
|
|
std::scoped_lock lk(g_i2c_mutex);
|
|
|
|
g_i2c_manager = std::move(sp);
|
|
|
|
AMS_ABORT_UNLESS(g_i2c_count == 0);
|
|
|
|
g_i2c_count = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::scoped_lock lk(g_i2c_pcv_mutex);
|
2021-10-07 00:20:48 +02:00
|
|
|
g_i2c_pcv_manager = std::move(sp_pcv);
|
2020-10-31 12:55:11 +01:00
|
|
|
AMS_ABORT_UNLESS(g_i2c_pcv_count == 0);
|
|
|
|
g_i2c_pcv_count = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_initialize_count = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeEmpty() {
|
|
|
|
std::scoped_lock lk(g_init_mutex);
|
|
|
|
|
|
|
|
++g_initialize_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Finalize() {
|
|
|
|
std::scoped_lock lk(g_init_mutex);
|
|
|
|
|
|
|
|
AMS_ASSERT(g_initialize_count > 0);
|
|
|
|
|
|
|
|
if ((--g_initialize_count) == 0) {
|
|
|
|
{
|
|
|
|
std::scoped_lock lk(g_i2c_mutex);
|
|
|
|
AMS_ASSERT(g_i2c_count > 0);
|
2020-11-10 14:02:41 +01:00
|
|
|
if (g_i2c_count > 0) {
|
|
|
|
if ((--g_i2c_count) == 0) {
|
2021-01-17 16:55:32 +01:00
|
|
|
g_i2c_manager = nullptr;
|
2020-11-10 14:02:41 +01:00
|
|
|
}
|
2020-10-31 12:55:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::scoped_lock lk(g_i2c_pcv_mutex);
|
|
|
|
AMS_ASSERT(g_i2c_pcv_count > 0);
|
2020-11-10 14:02:41 +01:00
|
|
|
if (g_i2c_pcv_count > 0) {
|
|
|
|
if ((--g_i2c_pcv_count) == 0) {
|
2021-01-17 16:55:32 +01:00
|
|
|
g_i2c_manager = nullptr;
|
2020-11-10 14:02:41 +01:00
|
|
|
}
|
2020-10-31 12:55:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 05:50:21 +01:00
|
|
|
Result OpenSession(I2cSession *out, DeviceCode device_code) {
|
|
|
|
/* Get manager for the device. */
|
|
|
|
auto manager = GetManager(device_code);
|
|
|
|
|
|
|
|
/* Get the session. */
|
2021-01-17 16:55:32 +01:00
|
|
|
ams::sf::SharedPointer<i2c::sf::ISession> session;
|
2020-11-01 05:50:21 +01:00
|
|
|
{
|
|
|
|
if (hos::GetVersion() >= hos::Version_6_0_0) {
|
2021-01-17 16:55:32 +01:00
|
|
|
R_TRY(manager->OpenSession2(std::addressof(session), device_code));
|
2020-11-01 05:50:21 +01:00
|
|
|
} else {
|
2021-01-17 16:55:32 +01:00
|
|
|
R_TRY(manager->OpenSession(std::addressof(session), ConvertToI2cDevice(device_code)));
|
2020-11-01 05:50:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set output. */
|
2021-01-17 16:55:32 +01:00
|
|
|
out->_session = session.Detach();
|
2020-11-01 05:50:21 +01:00
|
|
|
|
|
|
|
/* We succeeded. */
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloseSession(I2cSession &session) {
|
|
|
|
/* Close the session. */
|
2021-01-17 16:55:32 +01:00
|
|
|
ams::sf::ReleaseSharedObject(GetInterface(session));
|
2020-11-01 05:50:21 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-11-01 08:52:02 +01:00
|
|
|
void SetRetryPolicy(const I2cSession &session, int max_retry_count, int retry_interval_us) {
|
2020-11-01 05:50:21 +01:00
|
|
|
AMS_ASSERT(max_retry_count >= 0);
|
2020-11-01 08:52:02 +01:00
|
|
|
AMS_ASSERT(retry_interval_us >= 0);
|
2020-11-01 05:50:21 +01:00
|
|
|
|
2020-11-01 08:52:02 +01:00
|
|
|
R_ABORT_UNLESS(GetInterface(session)->SetRetryPolicy(max_retry_count, retry_interval_us));
|
2020-11-01 05:50:21 +01:00
|
|
|
}
|
|
|
|
|
2020-10-31 12:55:11 +01:00
|
|
|
}
|