mode_ds4.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // mode_ds4.c — DualShock 4 / HID-gyro emulated controller (ported from OpenPuck
  2. // mode_hidgyro.cpp). Report id 0x01 (63B) + gyro/accel + split trackpad; feature
  3. // reports for motion calibration / MAC / fw; rumble on output report 0x05.
  4. //
  5. // SPDX-License-Identifier: AGPL-3.0-or-later
  6. #include "puck/emu.h"
  7. #include "puck/gamepad_util.h"
  8. #include "puck/relay.h"
  9. #include <string.h>
  10. #include "hid_reports.h"
  11. #define DS4_TOUCH_H 942
  12. #define DS4_STATUS_USB 0x1B
  13. static uint16_t ds4_build(int slot, uint8_t *out, uint8_t *rid)
  14. {
  15. *rid = 0x01;
  16. uint32_t b = g_in[slot].buttons;
  17. bool lt_touch = (b & TB_LPADT) || (b & TB_LPADC);
  18. bool rt_touch = (b & TB_RPADT) || (b & TB_RPADC);
  19. memset(out, 0, 63);
  20. out[0] = sw_stick(g_in[slot].lx, false);
  21. out[1] = sw_stick(g_in[slot].ly, true);
  22. out[2] = sw_stick(g_in[slot].rx, false);
  23. out[3] = sw_stick(g_in[slot].ry, true);
  24. out[4] = ps_hat_nibble(b) | ps_face_nibble(b);
  25. out[5] = ps_shoulders_byte(b, g_in[slot].lt, g_in[slot].rt);
  26. static uint8_t ctr;
  27. out[6] = ((ctr++ & 0x0F) << 4) |
  28. ((b & (TB_TOUCH | TB_LPADC | TB_RPADC)) ? 0x02 : 0) |
  29. ((b & TB_STEAM) ? 0x01 : 0);
  30. out[7] = g_in[slot].lt;
  31. out[8] = g_in[slot].rt;
  32. out[12] = g_in[slot].gx & 0xFF;
  33. out[13] = g_in[slot].gx >> 8;
  34. out[14] = g_in[slot].gz & 0xFF;
  35. out[15] = g_in[slot].gz >> 8;
  36. out[16] = (-g_in[slot].gy) & 0xFF;
  37. out[17] = (-g_in[slot].gy) >> 8;
  38. out[18] = g_in[slot].ax & 0xFF;
  39. out[19] = g_in[slot].ax >> 8;
  40. out[20] = g_in[slot].ay & 0xFF;
  41. out[21] = g_in[slot].ay >> 8;
  42. out[22] = g_in[slot].az & 0xFF;
  43. out[23] = g_in[slot].az >> 8;
  44. out[29] = DS4_STATUS_USB;
  45. if (lt_touch || rt_touch) {
  46. uint16_t lx, ly, rx, ry;
  47. steam_pads_to_touch(b, DS4_TOUCH_H, g_in[slot].lpx,
  48. g_in[slot].lpy, g_in[slot].rpx,
  49. g_in[slot].rpy, &lx, &ly, &rx, &ry);
  50. static uint8_t tstamp;
  51. out[32] = 1;
  52. out[33] = tstamp++;
  53. touch_pack_pads(out + 34, lt_touch, rt_touch, lx, ly, rx, ry);
  54. } else {
  55. out[32] = 0;
  56. touch_pack_pads(out + 34, false, false, 0, 0, 0, 0);
  57. }
  58. return 63;
  59. }
  60. static uint16_t ds4_get(int slot, uint8_t rid, uint8_t type, uint8_t *buf,
  61. uint16_t reqlen)
  62. {
  63. if (type != PP_HID_FEATURE || reqlen == 0)
  64. return 0;
  65. memset(buf, 0, reqlen);
  66. uint8_t mac[6] = {
  67. 0x00, 0x1B, 0xDC, 0x4F, 0x55, (uint8_t)(0x50 + slot)
  68. };
  69. switch (rid) {
  70. case 0x02:
  71. if (reqlen < 36)
  72. return 0;
  73. ps_neutral_calib(buf);
  74. return 36;
  75. case 0x12:
  76. if (reqlen < 15)
  77. return 0;
  78. memcpy(buf, mac, 6);
  79. return 15;
  80. case 0x81:
  81. if (reqlen < 6)
  82. return 0;
  83. memcpy(buf, mac, 6);
  84. return 6;
  85. case 0xA3:
  86. if (reqlen < 48)
  87. return 0;
  88. buf[0] = 0x01;
  89. return 48;
  90. default:
  91. return 0;
  92. }
  93. }
  94. static void ds4_set(int slot, uint8_t rid, uint8_t type, const uint8_t *b,
  95. uint16_t n)
  96. {
  97. if (type != PP_HID_OUTPUT)
  98. return;
  99. // OUT endpoint delivers rid=0 with the id in b[0]; control SET_REPORT splits
  100. // the id into rid. Normalise to p = bytes AFTER the id (effects @ [3]/[4]).
  101. const uint8_t *p;
  102. uint16_t pn;
  103. if (rid == 0) {
  104. if (n < 1 || b[0] != 0x05)
  105. return;
  106. p = b + 1;
  107. pn = (uint16_t)(n - 1);
  108. } else if (rid == 0x05) {
  109. p = b;
  110. pn = n;
  111. } else {
  112. return;
  113. }
  114. if (pn < 5)
  115. return;
  116. puck_rumble(slot, (uint16_t)p[4] * 257u, (uint16_t)p[3] * 257u);
  117. }
  118. const emu_mode_t emu_ds4 = {
  119. .vid = 0x054C,
  120. .pid = 0x05C4,
  121. .bcd = 0x0120,
  122. .product = "Wireless Controller",
  123. .report_desc = GYRO_HID_DESC,
  124. .report_desc_len = sizeof(GYRO_HID_DESC),
  125. .build = ds4_build,
  126. .get_report = ds4_get,
  127. .set_report = ds4_set,
  128. .poll_ms = 4,
  129. };