Monado OpenXR Runtime
Loading...
Searching...
No Matches
pssense_protocol.h
Go to the documentation of this file.
1// Copyright 2023, Collabora, Ltd.
2// Copyright 2023, Jarett Millard
3// Copyright 2026, Beyley Cardellio
4// SPDX-License-Identifier: BSL-1.0
5/*!
6 * @file
7 * @brief PlayStation Sense controller prober and driver code.
8 * @author Jarett Millard <jarett.millard@gmail.com>
9 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
10 * @ingroup drv_pssense
11 */
12
13#pragma once
14
15#include "xrt/xrt_byte_order.h"
16
17#include "math/m_api.h"
18
19
20#pragma pack(push, 1)
21
22// @todo Remove when clang-format is updated in CI
23// clang-format off
24// IMU ticks are in thirds of a microsecond, so 1 tick = 333.333... nanoseconds
25#define NS_TO_IMU_TICKS(ns) (((uint64_t)(ns) * 3) / 1000)
26#define IMU_TICKS_TO_NS(ticks) (((uint64_t)(ticks) * 1000) / 3)
27
28#define PERIOD_ID_TO_DURATION_NS(period_id) (time_duration_ns)(IMU_TICKS_TO_NS(150LLU * (uint64_t)period_id))
29// @note: the +1 is to ensure things round correctly when dealing with converting to and back
30#define DURATION_NS_TO_PERIOD_ID(duration_ns) (uint8_t)(((NS_TO_IMU_TICKS(duration_ns + 1)) / 150LLU) & 0xFF)
31// clang-format on
32
33#define STABLE_MIN_PERIOD_ID 20
34
35#define MIN_PERIOD_ID 1
36#define MAX_PERIOD_ID 42
37
38#define PCM_SAMPLE_RATE 3000
39#define PCM_HAPTIC_BUF_SIZE 32
40
41#define INPUT_REPORT_ID_USB 0x01
42#define INPUT_REPORT_ID_BLUETOOTH 0x31
43
44#define OUTPUT_REPORT_ID_BLUETOOTH 0x31
45#define OUTPUT_REPORT_TAG 0x10
46
47#define CALIBRATION_DATA_FEATURE_REPORT_ID 0x05
48
49#define CALIBRATION_DATA_PART_ID_1 0
50#define CALIBRATION_DATA_PART_ID_2 0x81
51
52const uint8_t INPUT_REPORT_CRC32_SEED = 0xa1;
53const uint8_t OUTPUT_REPORT_CRC32_SEED = 0xa2;
54const uint8_t FEATURE_REPORT_CRC32_SEED = 0xa3;
55
56const uint8_t CHARGE_STATE_DISCHARGING = 0x00;
57const uint8_t CHARGE_STATE_CHARGING = 0x01;
58const uint8_t CHARGE_STATE_FULL = 0x02;
59const uint8_t CHARGE_STATE_ABNORMAL_VOLTAGE = 0x0A;
60const uint8_t CHARGE_STATE_ABNORMAL_TEMP = 0x0B;
61const uint8_t CHARGE_STATE_CHARGING_ERROR = 0x0F;
62
63#define INPUT_REPORT_BLUETOOTH_LENGTH 78
64#define INPUT_REPORT_USB_LENGTH 64
65
67{
68 uint8_t thumbstick_x;
69 uint8_t thumbstick_y;
70 uint8_t trigger_value;
71 uint8_t trigger_proximity;
72 uint8_t squeeze_proximity;
73 uint8_t unknown1[2]; // Always 0x0001
74 uint8_t buttons[3];
75 uint8_t unknown2; // Always 0x00
76 __le32 seq_no;
77 __le16 gyro[3];
78 __le16 accel[3];
79 __le32 imu_ticks;
80 uint8_t temperature;
81 uint8_t unknown3[7];
82 uint8_t trigger_feedback_state;
83 uint8_t trigger_feedback_mode;
84 uint8_t battery_state; // Low nibble charge level 0x00-0x0a, high nibble battery state
85 uint8_t plug_state; // Flags for USB data and/or power connected
86 __le32 host_timestamp;
87 __le32 device_timestamp_ticks;
88 uint8_t unknown4[4];
89 uint8_t aes_cmac[8];
90};
91
93{
94 uint8_t report_id;
95 struct pssense_input_report_common common;
96};
97static_assert(sizeof(struct pssense_usb_input_report) == INPUT_REPORT_USB_LENGTH,
98 "Incorrect input report struct length");
99
100/*!
101 * HID input report data packet.
102 */
104{
105 uint8_t report_id;
106 uint8_t bt_header;
107 struct pssense_input_report_common common;
108 uint8_t unknown5;
109 uint8_t crc_failure_count;
110 uint8_t padding[7];
111 __le32 crc;
112};
113static_assert(sizeof(struct pssense_bluetooth_input_report) == INPUT_REPORT_BLUETOOTH_LENGTH,
114 "Incorrect input report struct length");
115
116enum pssense_output_settings_flag1
117{
118 PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK0 = 1 << 0,
119 PSSENSE_OUTPUT_SETTINGS_FLAG1_RUMBLE_EMULATION = 1 << 1,
120 PSSENSE_OUTPUT_SETTINGS_FLAG1_ADAPTIVE_TRIGGER_ENABLE = 1 << 2,
121 PSSENSE_OUTPUT_SETTINGS_FLAG1_INTENSITY_INCREASE_SET_ENABLE = 1 << 3,
122 PSSENSE_OUTPUT_SETTINGS_FLAG1_INTENSITY_REDUCTION_SET_ENABLE = 1 << 4,
123 PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK5 = 1 << 5,
124 PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK6 = 1 << 6,
125 PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK7 = 1 << 7,
126};
127
128#define OUTPUT_SETTINGS_ENABLE_VIBRATION_BITS \
129 (PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK0 | PSSENSE_OUTPUT_SETTINGS_FLAG1_RUMBLE_EMULATION)
130
131#define OUTPUT_SETTINGS_VIBRATE_MODE_HIGH_120HZ 0x00
132// 0x20
133#define OUTPUT_SETTINGS_VIBRATE_MODE_LOW_60HZ (PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK5)
134// 0x40
135#define OUTPUT_SETTINGS_VIBRATE_MODE_CLASSIC_RUMBLE (PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK6)
136// 0x60
137#define OUTPUT_SETTINGS_VIBRATE_MODE_DIET_RUMBLE \
138 (PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK5 | PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK6)
139
141{
142 PSSENSE_OUTPUT_SETTINGS_FLAG2_UNK0 = 1 << 0,
143 PSSENSE_OUTPUT_SETTINGS_FLAG2_UNK1 = 1 << 1,
144 //! Used to mark whether to read the status LED enable bool or not.
146 PSSENSE_OUTPUT_SETTINGS_FLAG2_UNK3 = 1 << 3,
147 PSSENSE_OUTPUT_SETTINGS_FLAG2_UNK4 = 1 << 4,
148 PSSENSE_OUTPUT_SETTINGS_FLAG2_UNK5 = 1 << 5,
149 PSSENSE_OUTPUT_SETTINGS_FLAG2_UNK6 = 1 << 6,
150 PSSENSE_OUTPUT_SETTINGS_FLAG2_UNK7 = 1 << 7,
151};
152
153enum pssense_adaptive_trigger_mode
154{
155 TRIGGER_FEEDBACK_MODE_OFF = 0x05,
156
157 // simple versions of other effects, shouldn't be used
158 TRIGGER_FEEDBACK_MODE_SIMPLE_FEEDBACK = 0x01,
159 TRIGGER_FEEDBACK_MODE_SIMPLE_WEAPON = 0x02,
160 TRIGGER_FEEDBACK_MODE_SIMPLE_VIBRATION = 0x06,
161
162 // limited versions of official
163 TRIGGER_FEEDBACK_MODE_LIMITED_FEEDBACK = 0x11,
164 TRIGGER_FEEDBACK_MODE_LIMITED_WEAPON = 0x12,
165
166 // official ones found through reverse engineering
167 TRIGGER_FEEDBACK_MODE_FEEDBACK = 0x21,
168 TRIGGER_FEEDBACK_MODE_SLOPE_FEEDBACK = 0x22, // aka Bow
169 TRIGGER_FEEDBACK_MODE_WEAPON = 0x25,
170 TRIGGER_FEEDBACK_MODE_VIBRATION = 0x26,
171
172 // unofficial ones found through fuzzing firmware
173 TRIGGER_FEEDBACK_MODE_GALLOPING = 0x23,
174 TRIGGER_FEEDBACK_MODE_MACHINE = 0x27,
175};
176
178{
179 uint8_t mode; // See enum pssense_adaptive_trigger_mode
180 union {
181 uint8_t raw_parameters[10];
182 };
183};
184
185enum pssense_led_sync_phase
186{
187 // initializing
188 LED_SYNC_PHASE_INIT = 0,
189 // ???
190 LED_SYNC_PHASE_PRESCAN = 1,
191 // ???
192 LED_SYNC_PHASE_BROAD = 2,
193 // ???
194 LED_SYNC_PHASE_BG = 3,
195 // ???
196 LED_SYNC_PHASE_STABLE = 4,
197 // used during passthrough to prevent LEDs from bleeding into it
198 LED_SYNC_PHASE_LED_ALL_OFF = 5,
199 // probably 100% duty cycle?
200 LED_SYNC_PHASE_LED_ALL_ON = 6,
201 // probably also 100% duty cycle?
202 LED_SYNC_PHASE_DEBUG = 7,
203};
204
206{
207 uint8_t phase;
208 uint8_t sequence_number;
209 uint8_t period_id;
210 //! The position, in IMU ticks, @ref NS_PER_IMU_TICK
212 //! The length, in thirds of a nanosecond.
214 uint8_t led_blink[4];
215};
216
218{
219 //! See enum pssense_output_settings_flag1
220 uint8_t flag1;
221 //! See enum pssense_output_settings_flag2
222 uint8_t flag2;
223 //! Vibration amplitude from 0x00-0xff. Sending 0 turns vibration off.
225 //! Sony driver sometimes sets to 0x82.
226 uint8_t unk0;
227 //! Settings for the adaptive trigger
229 //! Time of packet send, in host time, in microseconds
231 //! Settings of the tracking LEDs.
233 //! Lower 4 bits for haptics reduction, upper 4 bits for trigger reduction. Decreases in 12.5% increments.
235 //! Whether to enable the status LED or not.
237 uint8_t unk1[2];
238};
239static_assert(sizeof(struct pssense_output_settings) == 38, "Incorrect output settings struct length");
240
241#define OUTPUT_REPORT_LENGTH_PS5 78
242#define OUTPUT_REPORT_LENGTH_USB 39
243
244/**
245 * HID output report data packet matching the PS5 layout, with PCM haptics.
246 *
247 * Reference:
248 * https://github.com/BnuuySolutions/PSVR2Toolkit/blob/b6ffe9f03cb2456d9d07aea74e839d9c5fd188f5/projects/psvr2_openvr_driver_ex/sense_controller.h#L44
249 */
251{
252 uint8_t report_id;
253 uint8_t seq_no_mode; // High bits only; low bits are always 0
254 uint8_t tag; // Needs to be 0x10 for this report
255 struct pssense_output_settings settings;
256 uint8_t counter;
257 uint8_t haptics[PCM_HAPTIC_BUF_SIZE];
258 __le32 crc;
259};
260static_assert(sizeof(struct pssense_ps5_output_report) == OUTPUT_REPORT_LENGTH_PS5,
261 "Incorrect output report struct length");
262
264{
265 // @note: There appears to be no dedicated report ID field here. Setting this report ID to
266 // any non-zero number seems to work, though. Zero doesn't work.
267 // I believe this controller just expects strange report IDs over USB.
268 // If USB breaks when using hidapi, this is the first place to check.
269 uint8_t seq_no_mode;
270 struct pssense_output_settings settings;
271};
272static_assert(sizeof(struct pssense_usb_output_report) == OUTPUT_REPORT_LENGTH_USB,
273 "Incorrect output report struct length");
274
275#define FEATURE_REPORT_LENGTH 64
276#define CALIBRATION_DATA_LENGTH 116
277
278/**
279 * HID output report data packet.
280 */
282{
283 uint8_t report_id;
284 uint8_t part_id;
285 uint8_t data[CALIBRATION_DATA_LENGTH / 2];
286 __le32 crc;
287};
288static_assert(sizeof(struct pssense_feature_report) == FEATURE_REPORT_LENGTH, "Incorrect feature report struct length");
289
291{
292 int16_t accel_plus_x; // 0x00
293 uint8_t _pad0[4]; // 0x02-0x05
294 int16_t accel_minus_x; // 0x06
295 uint8_t _pad1[6]; // 0x08-0x0D
296 int16_t accel_plus_y; // 0x0E
297 uint8_t _pad2[4]; // 0x10-0x13
298 int16_t accel_minus_y; // 0x14
299 uint8_t _pad3[6]; // 0x16-0x1B
300 int16_t accel_plus_z; // 0x1C
301 uint8_t _pad4[4]; // 0x1E-0x21
302 int16_t accel_minus_z; // 0x22
303 uint8_t _pad5[6]; // 0x24-0x29
304 int16_t gyro_plus_y; // 0x2A
305 uint8_t _pad6[4]; // 0x2C-0x2F
306 int16_t gyro_minus_y; // 0x30
307 uint8_t _pad7[6]; // 0x32-0x37
308 int16_t gyro_plus_z; // 0x38
309
310 uint8_t _pad8[4]; // 0x3A-0x3D
311 int16_t gyro_minus_z; // 0x3E
312 int16_t gyro_bias_x; // 0x40
313 int16_t gyro_bias_y; // 0x42
314 int16_t gyro_bias_z; // 0x44
315 int16_t gyro_plus_x; // 0x46
316 uint8_t _pad9[4]; // 0x48-0x4B
317 int16_t gyro_minus_x; // 0x4C
318 int16_t gyro_speed_ref1; // 0x4E
319 int16_t gyro_speed_ref2; // 0x50
320 uint8_t _pad_end[34]; // 0x52-0x73
321};
322static_assert(sizeof(struct pssense_calibration_data) == CALIBRATION_DATA_LENGTH,
323 "Incorrect calibration data struct length");
324
325#pragma pack(pop)
C interface to math library.
pssense_output_settings_flag2
Definition pssense_protocol.h:141
@ PSSENSE_OUTPUT_SETTINGS_FLAG2_STATUS_LED_SET_ENABLE
Used to mark whether to read the status LED enable bool or not.
Definition pssense_protocol.h:145
HID input report data packet.
Definition pssense_protocol.h:104
Definition pssense_protocol.h:291
HID output report data packet.
Definition pssense_protocol.h:282
Definition pssense_protocol.h:67
Definition pssense_protocol.h:206
__le32 cycle_position
The position, in IMU ticks, NS_PER_IMU_TICK.
Definition pssense_protocol.h:211
__le32 cycle_length
The length, in thirds of a nanosecond.
Definition pssense_protocol.h:213
Definition pssense_protocol.h:178
Definition pssense_protocol.h:218
uint8_t flag2
See enum pssense_output_settings_flag2.
Definition pssense_protocol.h:222
struct pssense_led_settings led_settings
Settings of the tracking LEDs.
Definition pssense_protocol.h:232
__le32 host_timestamp_send_time_us
Time of packet send, in host time, in microseconds.
Definition pssense_protocol.h:230
uint8_t status_led_enable
Whether to enable the status LED or not.
Definition pssense_protocol.h:236
uint8_t vibration_amplitude
Vibration amplitude from 0x00-0xff. Sending 0 turns vibration off.
Definition pssense_protocol.h:224
uint8_t flag1
See enum pssense_output_settings_flag1.
Definition pssense_protocol.h:220
struct pssense_output_adaptive_trigger_settings trigger_settings
Settings for the adaptive trigger.
Definition pssense_protocol.h:228
uint8_t trigger_haptics_reduction
Lower 4 bits for haptics reduction, upper 4 bits for trigger reduction. Decreases in 12....
Definition pssense_protocol.h:234
uint8_t unk0
Sony driver sometimes sets to 0x82.
Definition pssense_protocol.h:226
HID output report data packet matching the PS5 layout, with PCM haptics.
Definition pssense_protocol.h:251
Definition pssense_protocol.h:93
Definition pssense_protocol.h:264
Endian-specific byte order defines.