mic_u.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/logging/log.h"
  5. #include "core/hle/kernel/event.h"
  6. #include "core/hle/kernel/kernel.h"
  7. #include "core/hle/kernel/shared_memory.h"
  8. #include "core/hle/service/mic_u.h"
  9. namespace Service {
  10. namespace MIC {
  11. enum class Encoding : u8 {
  12. PCM8 = 0,
  13. PCM16 = 1,
  14. PCM8Signed = 2,
  15. PCM16Signed = 3,
  16. };
  17. enum class SampleRate : u8 {
  18. SampleRate32730 = 0,
  19. SampleRate16360 = 1,
  20. SampleRate10910 = 2,
  21. SampleRate8180 = 3
  22. };
  23. static Kernel::SharedPtr<Kernel::Event> buffer_full_event;
  24. static Kernel::SharedPtr<Kernel::SharedMemory> shared_memory;
  25. static u8 mic_gain = 0;
  26. static bool mic_power = false;
  27. static bool is_sampling = false;
  28. static bool allow_shell_closed;
  29. static bool clamp = false;
  30. static Encoding encoding;
  31. static SampleRate sample_rate;
  32. static s32 audio_buffer_offset;
  33. static u32 audio_buffer_size;
  34. static bool audio_buffer_loop;
  35. /**
  36. * MIC::MapSharedMem service function
  37. * Inputs:
  38. * 0 : Header Code[0x00010042]
  39. * 1 : Shared-mem size
  40. * 2 : CopyHandleDesc
  41. * 3 : Shared-mem handle
  42. * Outputs:
  43. * 1 : Result of function, 0 on success, otherwise error code
  44. */
  45. static void MapSharedMem(Interface* self) {
  46. u32* cmd_buff = Kernel::GetCommandBuffer();
  47. u32 size = cmd_buff[1];
  48. Kernel::Handle mem_handle = cmd_buff[3];
  49. shared_memory = Kernel::g_handle_table.Get<Kernel::SharedMemory>(mem_handle);
  50. if (shared_memory) {
  51. shared_memory->name = "MIC_U:shared_memory";
  52. }
  53. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  54. LOG_WARNING(Service_MIC, "called, size=0x%X, mem_handle=0x%08X", size, mem_handle);
  55. }
  56. /**
  57. * MIC::UnmapSharedMem service function
  58. * Inputs:
  59. * 0 : Header Code[0x00020000]
  60. * Outputs:
  61. * 1 : Result of function, 0 on success, otherwise error code
  62. */
  63. static void UnmapSharedMem(Interface* self) {
  64. u32* cmd_buff = Kernel::GetCommandBuffer();
  65. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  66. LOG_WARNING(Service_MIC, "called");
  67. }
  68. /**
  69. * MIC::StartSampling service function
  70. * Inputs:
  71. * 0 : Header Code[0x00030140]
  72. * 1 : Encoding
  73. * 2 : SampleRate
  74. * 3 : Base offset for audio data in sharedmem
  75. * 4 : Size of the audio data in sharedmem
  76. * 5 : Loop at end of buffer
  77. * Outputs:
  78. * 1 : Result of function, 0 on success, otherwise error code
  79. */
  80. static void StartSampling(Interface* self) {
  81. u32* cmd_buff = Kernel::GetCommandBuffer();
  82. encoding = static_cast<Encoding>(cmd_buff[1] & 0xFF);
  83. sample_rate = static_cast<SampleRate>(cmd_buff[2] & 0xFF);
  84. audio_buffer_offset = cmd_buff[3];
  85. audio_buffer_size = cmd_buff[4];
  86. audio_buffer_loop = (cmd_buff[5] & 0xFF) != 0;
  87. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  88. is_sampling = true;
  89. LOG_WARNING(Service_MIC, "(STUBBED) called, encoding=%u, sample_rate=%u, "
  90. "audio_buffer_offset=%d, audio_buffer_size=%u, audio_buffer_loop=%u",
  91. static_cast<u32>(encoding), static_cast<u32>(sample_rate), audio_buffer_offset,
  92. audio_buffer_size, audio_buffer_loop);
  93. }
  94. /**
  95. * MIC::AdjustSampling service function
  96. * Inputs:
  97. * 0 : Header Code[0x00040040]
  98. * 1 : SampleRate
  99. * Outputs:
  100. * 1 : Result of function, 0 on success, otherwise error code
  101. */
  102. static void AdjustSampling(Interface* self) {
  103. u32* cmd_buff = Kernel::GetCommandBuffer();
  104. sample_rate = static_cast<SampleRate>(cmd_buff[1] & 0xFF);
  105. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  106. LOG_WARNING(Service_MIC, "(STUBBED) called, sample_rate=%u", static_cast<u32>(sample_rate));
  107. }
  108. /**
  109. * MIC::StopSampling service function
  110. * Inputs:
  111. * 0 : Header Code[0x00050000]
  112. * Outputs:
  113. * 1 : Result of function, 0 on success, otherwise error code
  114. */
  115. static void StopSampling(Interface* self) {
  116. u32* cmd_buff = Kernel::GetCommandBuffer();
  117. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  118. is_sampling = false;
  119. LOG_WARNING(Service_MIC, "(STUBBED) called");
  120. }
  121. /**
  122. * MIC::IsSampling service function
  123. * Inputs:
  124. * 0 : Header Code[0x00060000]
  125. * Outputs:
  126. * 1 : Result of function, 0 on success, otherwise error code
  127. * 2 : 0 = sampling, non-zero = sampling
  128. */
  129. static void IsSampling(Interface* self) {
  130. u32* cmd_buff = Kernel::GetCommandBuffer();
  131. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  132. cmd_buff[2] = is_sampling;
  133. LOG_WARNING(Service_MIC, "(STUBBED) called");
  134. }
  135. /**
  136. * MIC::GetBufferFullEvent service function
  137. * Inputs:
  138. * 0 : Header Code[0x00070000]
  139. * Outputs:
  140. * 1 : Result of function, 0 on success, otherwise error code
  141. * 3 : Event handle
  142. */
  143. static void GetBufferFullEvent(Interface* self) {
  144. u32* cmd_buff = Kernel::GetCommandBuffer();
  145. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  146. cmd_buff[3] = Kernel::g_handle_table.Create(buffer_full_event).MoveFrom();
  147. LOG_WARNING(Service_MIC, "(STUBBED) called");
  148. }
  149. /**
  150. * MIC::SetGain service function
  151. * Inputs:
  152. * 0 : Header Code[0x00080040]
  153. * 1 : Gain
  154. * Outputs:
  155. * 1 : Result of function, 0 on success, otherwise error code
  156. */
  157. static void SetGain(Interface* self) {
  158. u32* cmd_buff = Kernel::GetCommandBuffer();
  159. mic_gain = cmd_buff[1] & 0xFF;
  160. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  161. LOG_WARNING(Service_MIC, "(STUBBED) called, mic_gain=%u", mic_gain);
  162. }
  163. /**
  164. * MIC::GetGain service function
  165. * Inputs:
  166. * 0 : Header Code[0x00090000]
  167. * Outputs:
  168. * 1 : Result of function, 0 on success, otherwise error code
  169. * 2 : Gain
  170. */
  171. static void GetGain(Interface* self) {
  172. u32* cmd_buff = Kernel::GetCommandBuffer();
  173. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  174. cmd_buff[2] = mic_gain;
  175. LOG_WARNING(Service_MIC, "(STUBBED) called");
  176. }
  177. /**
  178. * MIC::SetPower service function
  179. * Inputs:
  180. * 0 : Header Code[0x000A0040]
  181. * 1 : Power (0 = off, 1 = on)
  182. * Outputs:
  183. * 1 : Result of function, 0 on success, otherwise error code
  184. */
  185. static void SetPower(Interface* self) {
  186. u32* cmd_buff = Kernel::GetCommandBuffer();
  187. mic_power = (cmd_buff[1] & 0xFF) != 0;
  188. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  189. LOG_WARNING(Service_MIC, "(STUBBED) called, mic_power=%u", mic_power);
  190. }
  191. /**
  192. * MIC::GetPower service function
  193. * Inputs:
  194. * 0 : Header Code[0x000B0000]
  195. * Outputs:
  196. * 1 : Result of function, 0 on success, otherwise error code
  197. * 2 : Power
  198. */
  199. static void GetPower(Interface* self) {
  200. u32* cmd_buff = Kernel::GetCommandBuffer();
  201. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  202. cmd_buff[2] = mic_power;
  203. LOG_WARNING(Service_MIC, "(STUBBED) called");
  204. }
  205. /**
  206. * MIC::SetIirFilterMic service function
  207. * Inputs:
  208. * 0 : Header Code[0x000C0042]
  209. * 1 : Size
  210. * 2 : (Size << 4) | 0xA
  211. * 3 : Pointer to IIR Filter Data
  212. * Outputs:
  213. * 1 : Result of function, 0 on success, otherwise error code
  214. */
  215. static void SetIirFilterMic(Interface* self) {
  216. u32* cmd_buff = Kernel::GetCommandBuffer();
  217. u32 size = cmd_buff[1];
  218. VAddr buffer = cmd_buff[3];
  219. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  220. LOG_WARNING(Service_MIC, "(STUBBED) called, size=0x%X, buffer=0x%08X", size, buffer);
  221. }
  222. /**
  223. * MIC::SetClamp service function
  224. * Inputs:
  225. * 0 : Header Code[0x000D0040]
  226. * 1 : Clamp (0 = don't clamp, non-zero = clamp)
  227. * Outputs:
  228. * 1 : Result of function, 0 on success, otherwise error code
  229. */
  230. static void SetClamp(Interface* self) {
  231. u32* cmd_buff = Kernel::GetCommandBuffer();
  232. clamp = (cmd_buff[1] & 0xFF) != 0;
  233. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  234. LOG_WARNING(Service_MIC, "(STUBBED) called, clamp=%u", clamp);
  235. }
  236. /**
  237. * MIC::GetClamp service function
  238. * Inputs:
  239. * 0 : Header Code[0x000E0000]
  240. * Outputs:
  241. * 1 : Result of function, 0 on success, otherwise error code
  242. * 2 : Clamp (0 = don't clamp, non-zero = clamp)
  243. */
  244. static void GetClamp(Interface* self) {
  245. u32* cmd_buff = Kernel::GetCommandBuffer();
  246. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  247. cmd_buff[2] = clamp;
  248. LOG_WARNING(Service_MIC, "(STUBBED) called");
  249. }
  250. /**
  251. * MIC::SetAllowShellClosed service function
  252. * Inputs:
  253. * 0 : Header Code[0x000D0040]
  254. * 1 : Sampling allowed while shell closed (0 = disallow, non-zero = allow)
  255. * Outputs:
  256. * 1 : Result of function, 0 on success, otherwise error code
  257. */
  258. static void SetAllowShellClosed(Interface* self) {
  259. u32* cmd_buff = Kernel::GetCommandBuffer();
  260. allow_shell_closed = (cmd_buff[1] & 0xFF) != 0;
  261. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  262. LOG_WARNING(Service_MIC, "(STUBBED) called, allow_shell_closed=%u", allow_shell_closed);
  263. }
  264. /**
  265. * MIC_U::SetClientVersion service function
  266. * Inputs:
  267. * 1 : Used SDK Version
  268. * Outputs:
  269. * 1 : Result of function, 0 on success, otherwise error code
  270. */
  271. static void SetClientVersion(Interface* self) {
  272. u32* cmd_buff = Kernel::GetCommandBuffer();
  273. const u32 version = cmd_buff[1];
  274. self->SetVersion(version);
  275. LOG_WARNING(Service_MIC, "(STUBBED) called, version: 0x%08X", version);
  276. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  277. }
  278. const Interface::FunctionInfo FunctionTable[] = {
  279. {0x00010042, MapSharedMem, "MapSharedMem"},
  280. {0x00020000, UnmapSharedMem, "UnmapSharedMem"},
  281. {0x00030140, StartSampling, "StartSampling"},
  282. {0x00040040, AdjustSampling, "AdjustSampling"},
  283. {0x00050000, StopSampling, "StopSampling"},
  284. {0x00060000, IsSampling, "IsSampling"},
  285. {0x00070000, GetBufferFullEvent, "GetBufferFullEvent"},
  286. {0x00080040, SetGain, "SetGain"},
  287. {0x00090000, GetGain, "GetGain"},
  288. {0x000A0040, SetPower, "SetPower"},
  289. {0x000B0000, GetPower, "GetPower"},
  290. {0x000C0042, SetIirFilterMic, "SetIirFilterMic"},
  291. {0x000D0040, SetClamp, "SetClamp"},
  292. {0x000E0000, GetClamp, "GetClamp"},
  293. {0x000F0040, SetAllowShellClosed, "SetAllowShellClosed"},
  294. {0x00100040, SetClientVersion, "SetClientVersion"},
  295. };
  296. MIC_U::MIC_U() {
  297. Register(FunctionTable);
  298. shared_memory = nullptr;
  299. buffer_full_event =
  300. Kernel::Event::Create(Kernel::ResetType::OneShot, "MIC_U::buffer_full_event");
  301. mic_gain = 0;
  302. mic_power = false;
  303. is_sampling = false;
  304. clamp = false;
  305. }
  306. MIC_U::~MIC_U() {
  307. shared_memory = nullptr;
  308. buffer_full_event = nullptr;
  309. }
  310. } // namespace MIC
  311. } // namespace Service