From 3677d395a296297e6ad2419fd46e4a2e9d83e6d5 Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Tue, 31 Jul 2018 13:53:23 +0200 Subject: [PATCH 01/12] Add bpc service header --- nx/include/switch/services/bpc.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 nx/include/switch/services/bpc.h diff --git a/nx/include/switch/services/bpc.h b/nx/include/switch/services/bpc.h new file mode 100644 index 00000000..96e6d870 --- /dev/null +++ b/nx/include/switch/services/bpc.h @@ -0,0 +1,14 @@ +/** + * @file bpc.h + * @brief Board power control (bpc) service IPC wrapper. + * @author XorTroll + * @copyright libnx Authors + */ +#pragma once +#include "../types.h" + +Result bpcInitialize(void); +void bpcExit(void); + +Result bpcShutdownSystem(void); +Result bpcRebootSystem(void); From 183ba3822b3a7ba830ff68e4e11b9c8ceda2b577 Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Tue, 31 Jul 2018 13:57:00 +0200 Subject: [PATCH 02/12] Add bpc IPC commands --- nx/source/services/bpc.c | 86 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 nx/source/services/bpc.c diff --git a/nx/source/services/bpc.c b/nx/source/services/bpc.c new file mode 100644 index 00000000..5e1dbe83 --- /dev/null +++ b/nx/source/services/bpc.c @@ -0,0 +1,86 @@ +#include "types.h" +#include "result.h" +#include "arm/atomics.h" +#include "kernel/ipc.h" +#include "services/bpc.h" +#include "services/sm.h" + +static Service g_bpcSrv; +static u64 g_refCnt; + +Result bpcInitialize(void) +{ + atomicIncrement64(&g_refCnt); + + if (serviceIsActive(&g_bpcSrv)) return 0; + + Result rc = 0; + + rc = smGetService(&g_bpcSrv, "bpc"); + + return rc; +} + +void apmExit(void) +{ + if (atomicDecrement64(&g_refCnt) == 0) + { + serviceClose(&g_bpcSrv); + } +} + +Result bpcShutdownSystem(void) +{ + IpcCommand c; + ipcInitialize(&c); + struct + { + u64 magic; + u64 cmd_id; + } * raw; + raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw->magic = SFCI_MAGIC; + raw->cmd_id = 0; + Result rc = 0; + rc = serviceIpcDispatch(&g_bpcSrv); + if(R_SUCCEEDED(rc)) + { + IpcParsedCommand r; + ipcParse(&r); + struct + { + u64 magic; + u64 result; + } *resp = r.Raw; + rc = resp->result; + } + return rc; +} + +Result bpcRebootSystem(void) +{ + IpcCommand c; + ipcInitialize(&c); + struct + { + u64 magic; + u64 cmd_id; + } * raw; + raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw->magic = SFCI_MAGIC; + raw->cmd_id = 1; + Result rc = 0; + rc = serviceIpcDispatch(&g_bpcSrv); + if(R_SUCCEEDED(rc)) + { + IpcParsedCommand r; + ipcParse(&r); + struct + { + u64 magic; + u64 result; + } *resp = r.Raw; + rc = resp->result; + } + return rc; +} From 0a77a95fae27ce6da31f380a2cbb19930da6f2e2 Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Tue, 31 Jul 2018 13:57:22 +0200 Subject: [PATCH 03/12] Fix name typo --- nx/source/services/bpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nx/source/services/bpc.c b/nx/source/services/bpc.c index 5e1dbe83..8622ff21 100644 --- a/nx/source/services/bpc.c +++ b/nx/source/services/bpc.c @@ -21,7 +21,7 @@ Result bpcInitialize(void) return rc; } -void apmExit(void) +void bpcExit(void) { if (atomicDecrement64(&g_refCnt) == 0) { From 6af9fb0592041c7bbc2097ddf4cbfe157d4c55dc Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Wed, 1 Aug 2018 00:49:49 +0200 Subject: [PATCH 04/12] Fix for 1.0.0 firmware --- nx/source/services/bpc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nx/source/services/bpc.c b/nx/source/services/bpc.c index 8622ff21..246d1202 100644 --- a/nx/source/services/bpc.c +++ b/nx/source/services/bpc.c @@ -15,8 +15,8 @@ Result bpcInitialize(void) if (serviceIsActive(&g_bpcSrv)) return 0; Result rc = 0; - - rc = smGetService(&g_bpcSrv, "bpc"); + if(!kernelAbove100()) rc = smGetService(&g_bpcSrv, "bpc:c"); + else rc = smGetService(&g_bpcSrv, "bpc"); return rc; } From 8ba0da972ad7a54b6e6f2bd31614bb5d1e145bda Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Wed, 1 Aug 2018 01:25:11 +0200 Subject: [PATCH 05/12] Fix again --- nx/source/services/bpc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nx/source/services/bpc.c b/nx/source/services/bpc.c index 246d1202..6a9cf7c3 100644 --- a/nx/source/services/bpc.c +++ b/nx/source/services/bpc.c @@ -2,6 +2,7 @@ #include "result.h" #include "arm/atomics.h" #include "kernel/ipc.h" +#include "kernel/detect.h" #include "services/bpc.h" #include "services/sm.h" @@ -15,8 +16,7 @@ Result bpcInitialize(void) if (serviceIsActive(&g_bpcSrv)) return 0; Result rc = 0; - if(!kernelAbove100()) rc = smGetService(&g_bpcSrv, "bpc:c"); - else rc = smGetService(&g_bpcSrv, "bpc"); + rc = smGetService(&g_bpcSrv, kernelAbove200() ? "bpc" : "bpc:c"); return rc; } From 04d16200c4adc9f0fff43a78913e3f712cf84b68 Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Thu, 9 Aug 2018 17:49:10 +0200 Subject: [PATCH 06/12] Fix spacing --- nx/source/services/bpc.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/nx/source/services/bpc.c b/nx/source/services/bpc.c index 6a9cf7c3..da89440d 100644 --- a/nx/source/services/bpc.c +++ b/nx/source/services/bpc.c @@ -11,11 +11,13 @@ static u64 g_refCnt; Result bpcInitialize(void) { + Result rc = 0; atomicIncrement64(&g_refCnt); - if (serviceIsActive(&g_bpcSrv)) return 0; + if (serviceIsActive(&g_bpcSrv)) + return 0; + - Result rc = 0; rc = smGetService(&g_bpcSrv, kernelAbove200() ? "bpc" : "bpc:c"); return rc; @@ -24,9 +26,7 @@ Result bpcInitialize(void) void bpcExit(void) { if (atomicDecrement64(&g_refCnt) == 0) - { serviceClose(&g_bpcSrv); - } } Result bpcShutdownSystem(void) @@ -37,7 +37,7 @@ Result bpcShutdownSystem(void) { u64 magic; u64 cmd_id; - } * raw; + } *raw; raw = ipcPrepareHeader(&c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 0; @@ -65,7 +65,7 @@ Result bpcRebootSystem(void) { u64 magic; u64 cmd_id; - } * raw; + } *raw; raw = ipcPrepareHeader(&c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 1; From a970e1c139d8838622a37ab9d5df193b158887ba Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Fri, 17 Aug 2018 13:12:55 +0200 Subject: [PATCH 07/12] Fix spacing according to libnx --- nx/source/services/bpc.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/nx/source/services/bpc.c b/nx/source/services/bpc.c index da89440d..f170142b 100644 --- a/nx/source/services/bpc.c +++ b/nx/source/services/bpc.c @@ -33,8 +33,7 @@ Result bpcShutdownSystem(void) { IpcCommand c; ipcInitialize(&c); - struct - { + struct { u64 magic; u64 cmd_id; } *raw; @@ -43,15 +42,16 @@ Result bpcShutdownSystem(void) raw->cmd_id = 0; Result rc = 0; rc = serviceIpcDispatch(&g_bpcSrv); - if(R_SUCCEEDED(rc)) - { + + if(R_SUCCEEDED(rc)) { IpcParsedCommand r; ipcParse(&r); - struct - { + + struct { u64 magic; u64 result; } *resp = r.Raw; + rc = resp->result; } return rc; @@ -61,25 +61,26 @@ Result bpcRebootSystem(void) { IpcCommand c; ipcInitialize(&c); - struct - { + struct { u64 magic; u64 cmd_id; } *raw; + raw = ipcPrepareHeader(&c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 1; Result rc = 0; + rc = serviceIpcDispatch(&g_bpcSrv); - if(R_SUCCEEDED(rc)) - { + if(R_SUCCEEDED(rc)) { IpcParsedCommand r; ipcParse(&r); - struct - { + + struct { u64 magic; u64 result; } *resp = r.Raw; + rc = resp->result; } return rc; From eb9486b16fed5dc6615cfa4f2ece1548165ff6f0 Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Fri, 17 Aug 2018 16:58:39 +0200 Subject: [PATCH 08/12] Add some missing newlines --- nx/source/services/bpc.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/nx/source/services/bpc.c b/nx/source/services/bpc.c index f170142b..532e44f5 100644 --- a/nx/source/services/bpc.c +++ b/nx/source/services/bpc.c @@ -33,15 +33,17 @@ Result bpcShutdownSystem(void) { IpcCommand c; ipcInitialize(&c); + struct { u64 magic; u64 cmd_id; } *raw; + raw = ipcPrepareHeader(&c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 0; - Result rc = 0; - rc = serviceIpcDispatch(&g_bpcSrv); + + Result rc = serviceIpcDispatch(&g_bpcSrv); if(R_SUCCEEDED(rc)) { IpcParsedCommand r; @@ -54,6 +56,7 @@ Result bpcShutdownSystem(void) rc = resp->result; } + return rc; } @@ -61,6 +64,7 @@ Result bpcRebootSystem(void) { IpcCommand c; ipcInitialize(&c); + struct { u64 magic; u64 cmd_id; @@ -69,9 +73,9 @@ Result bpcRebootSystem(void) raw = ipcPrepareHeader(&c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 1; - Result rc = 0; - rc = serviceIpcDispatch(&g_bpcSrv); + Result rc = serviceIpcDispatch(&g_bpcSrv); + if(R_SUCCEEDED(rc)) { IpcParsedCommand r; ipcParse(&r); @@ -83,5 +87,6 @@ Result bpcRebootSystem(void) rc = resp->result; } + return rc; } From 7768d22a6ce95ae75e362cc23128aae10216c9c4 Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Sat, 18 Aug 2018 12:51:13 +0200 Subject: [PATCH 09/12] Update bpc.c --- nx/source/services/bpc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nx/source/services/bpc.c b/nx/source/services/bpc.c index 532e44f5..c962832d 100644 --- a/nx/source/services/bpc.c +++ b/nx/source/services/bpc.c @@ -12,6 +12,7 @@ static u64 g_refCnt; Result bpcInitialize(void) { Result rc = 0; + atomicIncrement64(&g_refCnt); if (serviceIsActive(&g_bpcSrv)) @@ -40,6 +41,7 @@ Result bpcShutdownSystem(void) } *raw; raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw->magic = SFCI_MAGIC; raw->cmd_id = 0; From b71c3c9beed0d4e84e4ac1a561cb6e4c691285cf Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Sat, 18 Aug 2018 12:51:57 +0200 Subject: [PATCH 10/12] Update bpc.c --- nx/source/services/bpc.c | 1 - 1 file changed, 1 deletion(-) diff --git a/nx/source/services/bpc.c b/nx/source/services/bpc.c index c962832d..4be0c93c 100644 --- a/nx/source/services/bpc.c +++ b/nx/source/services/bpc.c @@ -18,7 +18,6 @@ Result bpcInitialize(void) if (serviceIsActive(&g_bpcSrv)) return 0; - rc = smGetService(&g_bpcSrv, kernelAbove200() ? "bpc" : "bpc:c"); return rc; From 461892122b72cd4118bf7ad09439fd2986f9fb2e Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Thu, 6 Sep 2018 18:55:39 +0200 Subject: [PATCH 11/12] Create psm.h --- nx/include/switch/services/psm.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 nx/include/switch/services/psm.h diff --git a/nx/include/switch/services/psm.h b/nx/include/switch/services/psm.h new file mode 100644 index 00000000..d233ee01 --- /dev/null +++ b/nx/include/switch/services/psm.h @@ -0,0 +1,13 @@ +/** + * @file psm.h + * @brief PSM service IPC wrapper. + * @author XorTroll + * @copyright libnx Authors + */ +#pragma once +#include "../types.h" + +Result psmInitialize(void); +void psmExit(void); + +Result psmGetBatteryChargePercentage(u32 *out); From ab33169116622ac2bd761adc2eca4de35a8a3efd Mon Sep 17 00:00:00 2001 From: XorTroll <33005497+XorTroll@users.noreply.github.com> Date: Thu, 6 Sep 2018 18:59:07 +0200 Subject: [PATCH 12/12] Create psm.c --- nx/source/services/psm.c | 67 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 nx/source/services/psm.c diff --git a/nx/source/services/psm.c b/nx/source/services/psm.c new file mode 100644 index 00000000..26b302cb --- /dev/null +++ b/nx/source/services/psm.c @@ -0,0 +1,67 @@ +#include "types.h" +#include "result.h" +#include "arm/atomics.h" +#include "kernel/ipc.h" +#include "kernel/detect.h" +#include "services/psm.h" +#include "services/sm.h" + +static Service g_psmSrv; +static u64 g_refCnt; + +Result psmInitialize(void) +{ + Result rc = 0; + + atomicIncrement64(&g_refCnt); + + if (serviceIsActive(&g_psmSrv)) + return 0; + + rc = smGetService(&g_psmSrv, "psm"); + + return rc; +} + +void psmExit(void) +{ + if (atomicDecrement64(&g_refCnt) == 0) + serviceClose(&g_psmSrv); +} + +Result psmGetBatteryChargePercentage(u32 *out) +{ + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + } *raw; + + raw = ipcPrepareHeader(&c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 0; + + Result rc = serviceIpcDispatch(&g_psmSrv); + + if(R_SUCCEEDED(rc)) { + IpcParsedCommand r; + ipcParse(&r); + + struct { + u64 magic; + u64 result; + u32 percentage; + } *resp = r.Raw; + + rc = resp->result; + + if (R_SUCCEEDED(rc)) { + *out = resp->percentage; + } + } + + return rc; +}