hidbus.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/logging/log.h"
  4. #include "common/settings.h"
  5. #include "core/core.h"
  6. #include "core/core_timing.h"
  7. #include "core/hid/hid_types.h"
  8. #include "core/hle/kernel/k_event.h"
  9. #include "core/hle/kernel/k_readable_event.h"
  10. #include "core/hle/kernel/k_shared_memory.h"
  11. #include "core/hle/kernel/k_transfer_memory.h"
  12. #include "core/hle/service/hid/hidbus.h"
  13. #include "core/hle/service/hid/hidbus/ringcon.h"
  14. #include "core/hle/service/hid/hidbus/starlink.h"
  15. #include "core/hle/service/hid/hidbus/stubbed.h"
  16. #include "core/hle/service/ipc_helpers.h"
  17. #include "core/hle/service/service.h"
  18. #include "core/memory.h"
  19. namespace Service::HID {
  20. // (15ms, 66Hz)
  21. constexpr auto hidbus_update_ns = std::chrono::nanoseconds{15 * 1000 * 1000};
  22. HidBus::HidBus(Core::System& system_)
  23. : ServiceFramework{system_, "hidbus"}, service_context{system_, service_name} {
  24. // clang-format off
  25. static const FunctionInfo functions[] = {
  26. {1, &HidBus::GetBusHandle, "GetBusHandle"},
  27. {2, &HidBus::IsExternalDeviceConnected, "IsExternalDeviceConnected"},
  28. {3, &HidBus::Initialize, "Initialize"},
  29. {4, &HidBus::Finalize, "Finalize"},
  30. {5, &HidBus::EnableExternalDevice, "EnableExternalDevice"},
  31. {6, &HidBus::GetExternalDeviceId, "GetExternalDeviceId"},
  32. {7, &HidBus::SendCommandAsync, "SendCommandAsync"},
  33. {8, &HidBus::GetSendCommandAsynceResult, "GetSendCommandAsynceResult"},
  34. {9, &HidBus::SetEventForSendCommandAsycResult, "SetEventForSendCommandAsycResult"},
  35. {10, &HidBus::GetSharedMemoryHandle, "GetSharedMemoryHandle"},
  36. {11, &HidBus::EnableJoyPollingReceiveMode, "EnableJoyPollingReceiveMode"},
  37. {12, &HidBus::DisableJoyPollingReceiveMode, "DisableJoyPollingReceiveMode"},
  38. {13, nullptr, "GetPollingData"},
  39. {14, &HidBus::SetStatusManagerType, "SetStatusManagerType"},
  40. };
  41. // clang-format on
  42. RegisterHandlers(functions);
  43. // Register update callbacks
  44. hidbus_update_event = Core::Timing::CreateEvent(
  45. "Hidbus::UpdateCallback",
  46. [this](std::uintptr_t user_data, s64 time,
  47. std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> {
  48. const auto guard = LockService();
  49. UpdateHidbus(user_data, ns_late);
  50. return std::nullopt;
  51. });
  52. system_.CoreTiming().ScheduleLoopingEvent(hidbus_update_ns, hidbus_update_ns,
  53. hidbus_update_event);
  54. }
  55. HidBus::~HidBus() {
  56. system.CoreTiming().UnscheduleEvent(hidbus_update_event, 0);
  57. }
  58. void HidBus::UpdateHidbus(std::uintptr_t user_data, std::chrono::nanoseconds ns_late) {
  59. if (is_hidbus_enabled) {
  60. for (std::size_t i = 0; i < devices.size(); ++i) {
  61. if (!devices[i].is_device_initializated) {
  62. continue;
  63. }
  64. auto& device = devices[i].device;
  65. device->OnUpdate();
  66. auto& cur_entry = hidbus_status.entries[devices[i].handle.internal_index];
  67. cur_entry.is_polling_mode = device->IsPollingMode();
  68. cur_entry.polling_mode = device->GetPollingMode();
  69. cur_entry.is_enabled = device->IsEnabled();
  70. u8* shared_memory = system.Kernel().GetHidBusSharedMem().GetPointer();
  71. std::memcpy(shared_memory + (i * sizeof(HidbusStatusManagerEntry)), &hidbus_status,
  72. sizeof(HidbusStatusManagerEntry));
  73. }
  74. }
  75. }
  76. std::optional<std::size_t> HidBus::GetDeviceIndexFromHandle(BusHandle handle) const {
  77. for (std::size_t i = 0; i < devices.size(); ++i) {
  78. const auto& device_handle = devices[i].handle;
  79. if (handle.abstracted_pad_id == device_handle.abstracted_pad_id &&
  80. handle.internal_index == device_handle.internal_index &&
  81. handle.player_number == device_handle.player_number &&
  82. handle.bus_type_id == device_handle.bus_type_id &&
  83. handle.is_valid == device_handle.is_valid) {
  84. return i;
  85. }
  86. }
  87. return std::nullopt;
  88. }
  89. void HidBus::GetBusHandle(HLERequestContext& ctx) {
  90. IPC::RequestParser rp{ctx};
  91. struct Parameters {
  92. Core::HID::NpadIdType npad_id;
  93. INSERT_PADDING_WORDS_NOINIT(1);
  94. BusType bus_type;
  95. u64 applet_resource_user_id;
  96. };
  97. static_assert(sizeof(Parameters) == 0x18, "Parameters has incorrect size.");
  98. const auto parameters{rp.PopRaw<Parameters>()};
  99. LOG_INFO(Service_HID, "called, npad_id={}, bus_type={}, applet_resource_user_id={}",
  100. parameters.npad_id, parameters.bus_type, parameters.applet_resource_user_id);
  101. bool is_handle_found = 0;
  102. std::size_t handle_index = 0;
  103. for (std::size_t i = 0; i < devices.size(); i++) {
  104. const auto& handle = devices[i].handle;
  105. if (!handle.is_valid) {
  106. continue;
  107. }
  108. if (static_cast<Core::HID::NpadIdType>(handle.player_number) == parameters.npad_id &&
  109. handle.bus_type_id == static_cast<u8>(parameters.bus_type)) {
  110. is_handle_found = true;
  111. handle_index = i;
  112. break;
  113. }
  114. }
  115. // Handle not found. Create a new one
  116. if (!is_handle_found) {
  117. for (std::size_t i = 0; i < devices.size(); i++) {
  118. if (devices[i].handle.is_valid) {
  119. continue;
  120. }
  121. devices[i].handle = {
  122. .abstracted_pad_id = static_cast<u8>(i),
  123. .internal_index = static_cast<u8>(i),
  124. .player_number = static_cast<u8>(parameters.npad_id),
  125. .bus_type_id = static_cast<u8>(parameters.bus_type),
  126. .is_valid = true,
  127. };
  128. handle_index = i;
  129. break;
  130. }
  131. }
  132. struct OutData {
  133. bool is_valid;
  134. INSERT_PADDING_BYTES(7);
  135. BusHandle handle;
  136. };
  137. static_assert(sizeof(OutData) == 0x10, "OutData has incorrect size.");
  138. const OutData out_data{
  139. .is_valid = true,
  140. .handle = devices[handle_index].handle,
  141. };
  142. IPC::ResponseBuilder rb{ctx, 6};
  143. rb.Push(ResultSuccess);
  144. rb.PushRaw(out_data);
  145. }
  146. void HidBus::IsExternalDeviceConnected(HLERequestContext& ctx) {
  147. IPC::RequestParser rp{ctx};
  148. const auto bus_handle_{rp.PopRaw<BusHandle>()};
  149. LOG_INFO(Service_HID,
  150. "Called, abstracted_pad_id={}, bus_type={}, internal_index={}, "
  151. "player_number={}, is_valid={}",
  152. bus_handle_.abstracted_pad_id, bus_handle_.bus_type_id, bus_handle_.internal_index,
  153. bus_handle_.player_number, bus_handle_.is_valid);
  154. const auto device_index = GetDeviceIndexFromHandle(bus_handle_);
  155. if (device_index) {
  156. const auto& device = devices[device_index.value()].device;
  157. const bool is_attached = device->IsDeviceActivated();
  158. IPC::ResponseBuilder rb{ctx, 3};
  159. rb.Push(ResultSuccess);
  160. rb.Push(is_attached);
  161. return;
  162. }
  163. LOG_ERROR(Service_HID, "Invalid handle");
  164. IPC::ResponseBuilder rb{ctx, 2};
  165. rb.Push(ResultUnknown);
  166. return;
  167. }
  168. void HidBus::Initialize(HLERequestContext& ctx) {
  169. IPC::RequestParser rp{ctx};
  170. const auto bus_handle_{rp.PopRaw<BusHandle>()};
  171. const auto applet_resource_user_id{rp.Pop<u64>()};
  172. LOG_INFO(Service_HID,
  173. "called, abstracted_pad_id={} bus_type={} internal_index={} "
  174. "player_number={} is_valid={}, applet_resource_user_id={}",
  175. bus_handle_.abstracted_pad_id, bus_handle_.bus_type_id, bus_handle_.internal_index,
  176. bus_handle_.player_number, bus_handle_.is_valid, applet_resource_user_id);
  177. is_hidbus_enabled = true;
  178. const auto device_index = GetDeviceIndexFromHandle(bus_handle_);
  179. if (device_index) {
  180. const auto entry_index = devices[device_index.value()].handle.internal_index;
  181. auto& cur_entry = hidbus_status.entries[entry_index];
  182. if (bus_handle_.internal_index == 0 && Settings::values.enable_ring_controller) {
  183. MakeDevice<RingController>(bus_handle_);
  184. devices[device_index.value()].is_device_initializated = true;
  185. devices[device_index.value()].device->ActivateDevice();
  186. cur_entry.is_in_focus = true;
  187. cur_entry.is_connected = true;
  188. cur_entry.is_connected_result = ResultSuccess;
  189. cur_entry.is_enabled = false;
  190. cur_entry.is_polling_mode = false;
  191. } else {
  192. MakeDevice<HidbusStubbed>(bus_handle_);
  193. devices[device_index.value()].is_device_initializated = true;
  194. cur_entry.is_in_focus = true;
  195. cur_entry.is_connected = false;
  196. cur_entry.is_connected_result = ResultSuccess;
  197. cur_entry.is_enabled = false;
  198. cur_entry.is_polling_mode = false;
  199. }
  200. std::memcpy(system.Kernel().GetHidBusSharedMem().GetPointer(), &hidbus_status,
  201. sizeof(hidbus_status));
  202. IPC::ResponseBuilder rb{ctx, 2};
  203. rb.Push(ResultSuccess);
  204. return;
  205. }
  206. LOG_ERROR(Service_HID, "Invalid handle");
  207. IPC::ResponseBuilder rb{ctx, 2};
  208. rb.Push(ResultUnknown);
  209. return;
  210. }
  211. void HidBus::Finalize(HLERequestContext& ctx) {
  212. IPC::RequestParser rp{ctx};
  213. const auto bus_handle_{rp.PopRaw<BusHandle>()};
  214. const auto applet_resource_user_id{rp.Pop<u64>()};
  215. LOG_INFO(Service_HID,
  216. "called, abstracted_pad_id={}, bus_type={}, internal_index={}, "
  217. "player_number={}, is_valid={}, applet_resource_user_id={}",
  218. bus_handle_.abstracted_pad_id, bus_handle_.bus_type_id, bus_handle_.internal_index,
  219. bus_handle_.player_number, bus_handle_.is_valid, applet_resource_user_id);
  220. const auto device_index = GetDeviceIndexFromHandle(bus_handle_);
  221. if (device_index) {
  222. const auto entry_index = devices[device_index.value()].handle.internal_index;
  223. auto& cur_entry = hidbus_status.entries[entry_index];
  224. auto& device = devices[device_index.value()].device;
  225. devices[device_index.value()].is_device_initializated = false;
  226. device->DeactivateDevice();
  227. cur_entry.is_in_focus = true;
  228. cur_entry.is_connected = false;
  229. cur_entry.is_connected_result = ResultSuccess;
  230. cur_entry.is_enabled = false;
  231. cur_entry.is_polling_mode = false;
  232. std::memcpy(system.Kernel().GetHidBusSharedMem().GetPointer(), &hidbus_status,
  233. sizeof(hidbus_status));
  234. IPC::ResponseBuilder rb{ctx, 2};
  235. rb.Push(ResultSuccess);
  236. return;
  237. }
  238. LOG_ERROR(Service_HID, "Invalid handle");
  239. IPC::ResponseBuilder rb{ctx, 2};
  240. rb.Push(ResultUnknown);
  241. return;
  242. }
  243. void HidBus::EnableExternalDevice(HLERequestContext& ctx) {
  244. IPC::RequestParser rp{ctx};
  245. struct Parameters {
  246. bool enable;
  247. INSERT_PADDING_BYTES_NOINIT(7);
  248. BusHandle bus_handle;
  249. u64 inval;
  250. u64 applet_resource_user_id;
  251. };
  252. static_assert(sizeof(Parameters) == 0x20, "Parameters has incorrect size.");
  253. const auto parameters{rp.PopRaw<Parameters>()};
  254. LOG_DEBUG(Service_HID,
  255. "called, enable={}, abstracted_pad_id={}, bus_type={}, internal_index={}, "
  256. "player_number={}, is_valid={}, inval={}, applet_resource_user_id{}",
  257. parameters.enable, parameters.bus_handle.abstracted_pad_id,
  258. parameters.bus_handle.bus_type_id, parameters.bus_handle.internal_index,
  259. parameters.bus_handle.player_number, parameters.bus_handle.is_valid, parameters.inval,
  260. parameters.applet_resource_user_id);
  261. const auto device_index = GetDeviceIndexFromHandle(parameters.bus_handle);
  262. if (device_index) {
  263. auto& device = devices[device_index.value()].device;
  264. device->Enable(parameters.enable);
  265. IPC::ResponseBuilder rb{ctx, 2};
  266. rb.Push(ResultSuccess);
  267. return;
  268. }
  269. LOG_ERROR(Service_HID, "Invalid handle");
  270. IPC::ResponseBuilder rb{ctx, 2};
  271. rb.Push(ResultUnknown);
  272. return;
  273. }
  274. void HidBus::GetExternalDeviceId(HLERequestContext& ctx) {
  275. IPC::RequestParser rp{ctx};
  276. const auto bus_handle_{rp.PopRaw<BusHandle>()};
  277. LOG_DEBUG(Service_HID,
  278. "called, abstracted_pad_id={}, bus_type={}, internal_index={}, player_number={}, "
  279. "is_valid={}",
  280. bus_handle_.abstracted_pad_id, bus_handle_.bus_type_id, bus_handle_.internal_index,
  281. bus_handle_.player_number, bus_handle_.is_valid);
  282. const auto device_index = GetDeviceIndexFromHandle(bus_handle_);
  283. if (device_index) {
  284. const auto& device = devices[device_index.value()].device;
  285. u32 device_id = device->GetDeviceId();
  286. IPC::ResponseBuilder rb{ctx, 3};
  287. rb.Push(ResultSuccess);
  288. rb.Push<u32>(device_id);
  289. return;
  290. }
  291. LOG_ERROR(Service_HID, "Invalid handle");
  292. IPC::ResponseBuilder rb{ctx, 2};
  293. rb.Push(ResultUnknown);
  294. return;
  295. }
  296. void HidBus::SendCommandAsync(HLERequestContext& ctx) {
  297. IPC::RequestParser rp{ctx};
  298. const auto data = ctx.ReadBuffer();
  299. const auto bus_handle_{rp.PopRaw<BusHandle>()};
  300. LOG_DEBUG(Service_HID,
  301. "called, data_size={}, abstracted_pad_id={}, bus_type={}, internal_index={}, "
  302. "player_number={}, is_valid={}",
  303. data.size(), bus_handle_.abstracted_pad_id, bus_handle_.bus_type_id,
  304. bus_handle_.internal_index, bus_handle_.player_number, bus_handle_.is_valid);
  305. const auto device_index = GetDeviceIndexFromHandle(bus_handle_);
  306. if (device_index) {
  307. auto& device = devices[device_index.value()].device;
  308. device->SetCommand(data);
  309. IPC::ResponseBuilder rb{ctx, 2};
  310. rb.Push(ResultSuccess);
  311. return;
  312. }
  313. LOG_ERROR(Service_HID, "Invalid handle");
  314. IPC::ResponseBuilder rb{ctx, 2};
  315. rb.Push(ResultUnknown);
  316. return;
  317. };
  318. void HidBus::GetSendCommandAsynceResult(HLERequestContext& ctx) {
  319. IPC::RequestParser rp{ctx};
  320. const auto bus_handle_{rp.PopRaw<BusHandle>()};
  321. LOG_DEBUG(Service_HID,
  322. "called, abstracted_pad_id={}, bus_type={}, internal_index={}, player_number={}, "
  323. "is_valid={}",
  324. bus_handle_.abstracted_pad_id, bus_handle_.bus_type_id, bus_handle_.internal_index,
  325. bus_handle_.player_number, bus_handle_.is_valid);
  326. const auto device_index = GetDeviceIndexFromHandle(bus_handle_);
  327. if (device_index) {
  328. const auto& device = devices[device_index.value()].device;
  329. const std::vector<u8> data = device->GetReply();
  330. const u64 data_size = ctx.WriteBuffer(data);
  331. IPC::ResponseBuilder rb{ctx, 4};
  332. rb.Push(ResultSuccess);
  333. rb.Push<u64>(data_size);
  334. return;
  335. }
  336. LOG_ERROR(Service_HID, "Invalid handle");
  337. IPC::ResponseBuilder rb{ctx, 2};
  338. rb.Push(ResultUnknown);
  339. return;
  340. };
  341. void HidBus::SetEventForSendCommandAsycResult(HLERequestContext& ctx) {
  342. IPC::RequestParser rp{ctx};
  343. const auto bus_handle_{rp.PopRaw<BusHandle>()};
  344. LOG_INFO(Service_HID,
  345. "called, abstracted_pad_id={}, bus_type={}, internal_index={}, player_number={}, "
  346. "is_valid={}",
  347. bus_handle_.abstracted_pad_id, bus_handle_.bus_type_id, bus_handle_.internal_index,
  348. bus_handle_.player_number, bus_handle_.is_valid);
  349. const auto device_index = GetDeviceIndexFromHandle(bus_handle_);
  350. if (device_index) {
  351. const auto& device = devices[device_index.value()].device;
  352. IPC::ResponseBuilder rb{ctx, 2, 1};
  353. rb.Push(ResultSuccess);
  354. rb.PushCopyObjects(device->GetSendCommandAsycEvent());
  355. return;
  356. }
  357. LOG_ERROR(Service_HID, "Invalid handle");
  358. IPC::ResponseBuilder rb{ctx, 2};
  359. rb.Push(ResultUnknown);
  360. return;
  361. };
  362. void HidBus::GetSharedMemoryHandle(HLERequestContext& ctx) {
  363. LOG_DEBUG(Service_HID, "called");
  364. IPC::ResponseBuilder rb{ctx, 2, 1};
  365. rb.Push(ResultSuccess);
  366. rb.PushCopyObjects(&system.Kernel().GetHidBusSharedMem());
  367. }
  368. void HidBus::EnableJoyPollingReceiveMode(HLERequestContext& ctx) {
  369. IPC::RequestParser rp{ctx};
  370. const auto t_mem_size{rp.Pop<u32>()};
  371. const auto t_mem_handle{ctx.GetCopyHandle(0)};
  372. const auto polling_mode_{rp.PopEnum<JoyPollingMode>()};
  373. const auto bus_handle_{rp.PopRaw<BusHandle>()};
  374. ASSERT_MSG(t_mem_size == 0x1000, "t_mem_size is not 0x1000 bytes");
  375. auto t_mem = system.ApplicationProcess()->GetHandleTable().GetObject<Kernel::KTransferMemory>(
  376. t_mem_handle);
  377. if (t_mem.IsNull()) {
  378. LOG_ERROR(Service_HID, "t_mem is a nullptr for handle=0x{:08X}", t_mem_handle);
  379. IPC::ResponseBuilder rb{ctx, 2};
  380. rb.Push(ResultUnknown);
  381. return;
  382. }
  383. ASSERT_MSG(t_mem->GetSize() == 0x1000, "t_mem has incorrect size");
  384. LOG_INFO(Service_HID,
  385. "called, t_mem_handle=0x{:08X}, polling_mode={}, abstracted_pad_id={}, bus_type={}, "
  386. "internal_index={}, player_number={}, is_valid={}",
  387. t_mem_handle, polling_mode_, bus_handle_.abstracted_pad_id, bus_handle_.bus_type_id,
  388. bus_handle_.internal_index, bus_handle_.player_number, bus_handle_.is_valid);
  389. const auto device_index = GetDeviceIndexFromHandle(bus_handle_);
  390. if (device_index) {
  391. auto& device = devices[device_index.value()].device;
  392. device->SetPollingMode(polling_mode_);
  393. device->SetTransferMemoryAddress(t_mem->GetSourceAddress());
  394. IPC::ResponseBuilder rb{ctx, 2};
  395. rb.Push(ResultSuccess);
  396. return;
  397. }
  398. LOG_ERROR(Service_HID, "Invalid handle");
  399. IPC::ResponseBuilder rb{ctx, 2};
  400. rb.Push(ResultUnknown);
  401. return;
  402. }
  403. void HidBus::DisableJoyPollingReceiveMode(HLERequestContext& ctx) {
  404. IPC::RequestParser rp{ctx};
  405. const auto bus_handle_{rp.PopRaw<BusHandle>()};
  406. LOG_INFO(Service_HID,
  407. "called, abstracted_pad_id={}, bus_type={}, internal_index={}, player_number={}, "
  408. "is_valid={}",
  409. bus_handle_.abstracted_pad_id, bus_handle_.bus_type_id, bus_handle_.internal_index,
  410. bus_handle_.player_number, bus_handle_.is_valid);
  411. const auto device_index = GetDeviceIndexFromHandle(bus_handle_);
  412. if (device_index) {
  413. auto& device = devices[device_index.value()].device;
  414. device->DisablePollingMode();
  415. IPC::ResponseBuilder rb{ctx, 2};
  416. rb.Push(ResultSuccess);
  417. return;
  418. }
  419. LOG_ERROR(Service_HID, "Invalid handle");
  420. IPC::ResponseBuilder rb{ctx, 2};
  421. rb.Push(ResultUnknown);
  422. return;
  423. }
  424. void HidBus::SetStatusManagerType(HLERequestContext& ctx) {
  425. IPC::RequestParser rp{ctx};
  426. const auto manager_type{rp.PopEnum<StatusManagerType>()};
  427. LOG_WARNING(Service_HID, "(STUBBED) called, manager_type={}", manager_type);
  428. IPC::ResponseBuilder rb{ctx, 2};
  429. rb.Push(ResultSuccess);
  430. };
  431. } // namespace Service::HID