From 040767ce57c98982ce9d63fd41fd993ba4545de8 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Fri, 18 Oct 2019 22:45:24 -0700 Subject: [PATCH] clkrst: update for new-ipc --- nx/include/switch/services/clkrst.h | 3 +- nx/source/services/clkrst.c | 145 ++++------------------------ 2 files changed, 20 insertions(+), 128 deletions(-) diff --git a/nx/include/switch/services/clkrst.h b/nx/include/switch/services/clkrst.h index 02054176..5b059fdf 100644 --- a/nx/include/switch/services/clkrst.h +++ b/nx/include/switch/services/clkrst.h @@ -6,13 +6,14 @@ */ #pragma once #include "../types.h" -#include "../services/sm.h" +#include "../sf/service.h" #include "../services/pcv.h" typedef struct { Service s; } ClkrstSession; +/// Only available on [8.0.0+]. Result clkrstInitialize(void); void clkrstExit(void); Service* clkrstGetServiceSession(void); diff --git a/nx/source/services/clkrst.c b/nx/source/services/clkrst.c index 1840b151..1b137d55 100644 --- a/nx/source/services/clkrst.c +++ b/nx/source/services/clkrst.c @@ -1,40 +1,22 @@ -#include "types.h" -#include "result.h" -#include "arm/atomics.h" -#include "kernel/ipc.h" +#define NX_SERVICE_ASSUME_NON_DOMAIN +#include "service_guard.h" #include "services/pcv.h" -#include "runtime/hosversion.h" -#include "services/sm.h" #include "services/clkrst.h" +#include "runtime/hosversion.h" static Service g_clkrstSrv; -static u64 g_refCnt; -Result clkrstInitialize(void) { - if(hosversionBefore(8,0,0)) { +NX_GENERATE_SERVICE_GUARD(clkrst); + +Result _clkrstInitialize(void) { + if(hosversionBefore(8,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); - } - atomicIncrement64(&g_refCnt); - - if (serviceIsActive(&g_clkrstSrv)) { - return 0; - } - - Result rc = smGetService(&g_clkrstSrv, "clkrst"); - - if (R_FAILED(rc)) { - clkrstExit(); - } - - return rc; + return smGetService(&g_clkrstSrv, "clkrst"); } - -void clkrstExit(void) { - if (atomicDecrement64(&g_refCnt) == 0) { - serviceClose(&g_clkrstSrv); - } +void _clkrstCleanup(void) { + serviceClose(&g_clkrstSrv); } Service* clkrstGetServiceSession(void) { @@ -42,115 +24,24 @@ Service* clkrstGetServiceSession(void) { } Result clkrstOpenSession(ClkrstSession* session_out, PcvModuleId module_id, u32 unk) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; + const struct { u32 module_id; u32 unk; - } *raw; - - raw = ipcPrepareHeader(&c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 0; - raw->module_id = module_id; - raw->unk = unk; - - Result rc = serviceIpcDispatch(&g_clkrstSrv); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - ipcParse(&r); - - struct { - u64 magic; - u64 result; - } *resp = r.Raw; - - rc = resp->result; - if (R_SUCCEEDED(rc)) { - serviceCreate(&session_out->s, r.Handles[0]); - } - } - - return rc; + } in = { module_id, unk }; + return serviceDispatchIn(&g_clkrstSrv, 0, in, + .out_num_objects = 1, + .out_objects = &session_out->s, + ); } - void clkrstCloseSession(ClkrstSession* session) { serviceClose(&session->s); } Result clkrstSetClockRate(ClkrstSession* session, u32 hz) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - u32 hz; - } *raw; - - raw = serviceIpcPrepareHeader(&session->s, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 7; - raw->hz = hz; - - Result rc = serviceIpcDispatch(&session->s); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - } *resp; - - serviceIpcParse(&session->s, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - } - - return rc; + return serviceDispatchIn(&session->s, 7, hz); } Result clkrstGetClockRate(ClkrstSession* session, u32 *out_hz) { - IpcCommand c; - ipcInitialize(&c); - - struct { - u64 magic; - u64 cmd_id; - } *raw; - - raw = serviceIpcPrepareHeader(&session->s, &c, sizeof(*raw)); - - raw->magic = SFCI_MAGIC; - raw->cmd_id = 8; - - Result rc = serviceIpcDispatch(&session->s); - - if (R_SUCCEEDED(rc)) { - IpcParsedCommand r; - struct { - u64 magic; - u64 result; - u32 hz; - } *resp; - - serviceIpcParse(&session->s, &r, sizeof(*resp)); - resp = r.Raw; - - rc = resp->result; - - if (R_SUCCEEDED(rc)) { - *out_hz = resp->hz; - } - } - - return rc; + return serviceDispatchOut(&session->s, 8, *out_hz); }