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// clang-format on
28
29#define PCM_SAMPLE_RATE 3000
30#define PCM_HAPTIC_BUF_SIZE 32
31
32const uint8_t INPUT_REPORT_ID = 0x31;
33const uint8_t OUTPUT_REPORT_ID = 0x31;
34const uint8_t OUTPUT_REPORT_TAG = 0x10;
35const uint8_t CALIBRATION_DATA_FEATURE_REPORT_ID = 0x05;
36
37#define CALIBRATION_DATA_PART_ID_1 0
38#define CALIBRATION_DATA_PART_ID_2 0x81
39
40const uint8_t INPUT_REPORT_CRC32_SEED = 0xa1;
41const uint8_t OUTPUT_REPORT_CRC32_SEED = 0xa2;
42const uint8_t FEATURE_REPORT_CRC32_SEED = 0xa3;
43
44//! Gyro read value range is +-32768.
45const double PSSENSE_GYRO_SCALE_DEG = 180.0 / 1024;
46//! Accelerometer read value range is +-32768 and covers +-8 g.
48
49const uint8_t CHARGE_STATE_DISCHARGING = 0x00;
50const uint8_t CHARGE_STATE_CHARGING = 0x01;
51const uint8_t CHARGE_STATE_FULL = 0x02;
52const uint8_t CHARGE_STATE_ABNORMAL_VOLTAGE = 0x0A;
53const uint8_t CHARGE_STATE_ABNORMAL_TEMP = 0x0B;
54const uint8_t CHARGE_STATE_CHARGING_ERROR = 0x0F;
55
56#define INPUT_REPORT_LENGTH 78
57/*!
58 * HID input report data packet.
59 */
61{
62 uint8_t report_id;
63 uint8_t bt_header;
64 uint8_t thumbstick_x;
65 uint8_t thumbstick_y;
66 uint8_t trigger_value;
67 uint8_t trigger_proximity;
68 uint8_t squeeze_proximity;
69 uint8_t unknown1[2]; // Always 0x0001
70 uint8_t buttons[3];
71 uint8_t unknown2; // Always 0x00
72 __le32 seq_no;
73 __le16 gyro[3];
74 __le16 accel[3];
75 __le32 imu_ticks;
76 uint8_t temperature;
77 uint8_t unknown3[7];
78 uint8_t trigger_feedback_state;
79 uint8_t trigger_feedback_mode;
80 uint8_t battery_state; // High bits charge level 0x00-0x0a, low bits battery state
81 uint8_t plug_state; // Flags for USB data and/or power connected
82 __le32 host_timestamp;
83 __le32 device_timestamp_ticks;
84 uint8_t unknown4[4];
85 uint8_t aes_cmac[8];
86 uint8_t unknown5;
87 uint8_t crc_failure_count;
88 uint8_t padding[7];
89 __le32 crc;
90};
91static_assert(sizeof(struct pssense_input_report) == INPUT_REPORT_LENGTH, "Incorrect input report struct length");
92
93#define PS5_OUTPUT_REPORT_LENGTH 78
94
95enum pssense_output_settings_flag1
96{
97 PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK0 = 1 << 0,
98 PSSENSE_OUTPUT_SETTINGS_FLAG1_RUMBLE_EMULATION = 1 << 1,
99 PSSENSE_OUTPUT_SETTINGS_FLAG1_ADAPTIVE_TRIGGER_ENABLE = 1 << 2,
100 PSSENSE_OUTPUT_SETTINGS_FLAG1_INTENSITY_INCREASE_SET_ENABLE = 1 << 3,
101 PSSENSE_OUTPUT_SETTINGS_FLAG1_INTENSITY_REDUCTION_SET_ENABLE = 1 << 4,
102 PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK5 = 1 << 5,
103 PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK6 = 1 << 6,
104 PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK7 = 1 << 7,
105};
106
107#define OUTPUT_SETTINGS_ENABLE_VIBRATION_BITS \
108 (PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK0 | PSSENSE_OUTPUT_SETTINGS_FLAG1_RUMBLE_EMULATION)
109
110#define OUTPUT_SETTINGS_VIBRATE_MODE_HIGH_120HZ 0x00
111// 0x20
112#define OUTPUT_SETTINGS_VIBRATE_MODE_LOW_60HZ (PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK5)
113// 0x40
114#define OUTPUT_SETTINGS_VIBRATE_MODE_CLASSIC_RUMBLE (PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK6)
115// 0x60
116#define OUTPUT_SETTINGS_VIBRATE_MODE_DIET_RUMBLE \
117 (PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK5 | PSSENSE_OUTPUT_SETTINGS_FLAG1_UNK6)
118
120{
121 PSSENSE_OUTPUT_SETTINGS_FLAG2_UNK0 = 1 << 0,
122 PSSENSE_OUTPUT_SETTINGS_FLAG2_UNK1 = 1 << 1,
123 //! Used to mark whether to read the status LED enable bool or not.
125 PSSENSE_OUTPUT_SETTINGS_FLAG2_UNK3 = 1 << 3,
126 PSSENSE_OUTPUT_SETTINGS_FLAG2_UNK4 = 1 << 4,
127 PSSENSE_OUTPUT_SETTINGS_FLAG2_UNK5 = 1 << 5,
128 PSSENSE_OUTPUT_SETTINGS_FLAG2_UNK6 = 1 << 6,
129 PSSENSE_OUTPUT_SETTINGS_FLAG2_UNK7 = 1 << 7,
130};
131
132enum pssense_adaptive_trigger_mode
133{
134 TRIGGER_FEEDBACK_MODE_OFF = 0x05,
135
136 // simple versions of other effects, shouldn't be used
137 TRIGGER_FEEDBACK_MODE_SIMPLE_FEEDBACK = 0x01,
138 TRIGGER_FEEDBACK_MODE_SIMPLE_WEAPON = 0x02,
139 TRIGGER_FEEDBACK_MODE_SIMPLE_VIBRATION = 0x06,
140
141 // limited versions of official
142 TRIGGER_FEEDBACK_MODE_LIMITED_FEEDBACK = 0x11,
143 TRIGGER_FEEDBACK_MODE_LIMITED_WEAPON = 0x12,
144
145 // official ones found through reverse engineering
146 TRIGGER_FEEDBACK_MODE_FEEDBACK = 0x21,
147 TRIGGER_FEEDBACK_MODE_SLOPE_FEEDBACK = 0x22, // aka Bow
148 TRIGGER_FEEDBACK_MODE_WEAPON = 0x25,
149 TRIGGER_FEEDBACK_MODE_VIBRATION = 0x26,
150
151 // unofficial ones found through fuzzing firmware
152 TRIGGER_FEEDBACK_MODE_GALLOPING = 0x23,
153 TRIGGER_FEEDBACK_MODE_MACHINE = 0x27,
154};
155
157{
158 uint8_t mode; // See enum pssense_adaptive_trigger_mode
159 union {
160 uint8_t raw_parameters[10];
161 };
162};
163
164enum pssense_led_sync_phase
165{
166 // initializing
167 LED_SYNC_PHASE_INIT = 0,
168 // ???
169 LED_SYNC_PHASE_PRESCAN = 1,
170 // ???
171 LED_SYNC_PHASE_BROAD = 2,
172 // ???
173 LED_SYNC_PHASE_BG = 3,
174 // ???
175 LED_SYNC_PHASE_STABLE = 4,
176 // used during passthrough to prevent LEDs from bleeding into it
177 LED_SYNC_PHASE_LED_ALL_OFF = 5,
178 // probably 100% duty cycle?
179 LED_SYNC_PHASE_LED_ALL_ON = 6,
180 // probably also 100% duty cycle?
181 LED_SYNC_PHASE_DEBUG = 7,
182};
183
185{
186 uint8_t phase;
187 uint8_t sequence_number;
188 uint8_t period_id;
189 //! The position, in IMU ticks, @ref NS_PER_IMU_TICK
191 //! The length, in thirds of a nanosecond.
193 uint8_t led_blink[4];
194};
195
197{
198 //! See enum pssense_output_settings_flag1
199 uint8_t flag1;
200 //! See enum pssense_output_settings_flag2
201 uint8_t flag2;
202 //! Vibration amplitude from 0x00-0xff. Sending 0 turns vibration off.
204 //! Sony driver sometimes sets to 0x82.
205 uint8_t unk0;
206 //! Settings for the adaptive trigger
208 //! Time of packet send, in host time, in microseconds
210 //! Settings of the tracking LEDs.
212 //! Lower 4 bits for haptics reduction, upper 4 bits for trigger reduction. Decreases in 12.5% increments.
214 //! Whether to enable the status LED or not.
216 uint8_t unk1[2];
217};
218static_assert(sizeof(struct pssense_output_settings) == 38, "Incorrect output settings struct length");
219
220/**
221 * HID output report data packet matching the PS5 layout, with PCM haptics.
222 *
223 * Reference:
224 * https://github.com/BnuuySolutions/PSVR2Toolkit/blob/b6ffe9f03cb2456d9d07aea74e839d9c5fd188f5/projects/psvr2_openvr_driver_ex/sense_controller.h#L44
225 */
227{
228 uint8_t report_id;
229 uint8_t seq_no_mode; // High bits only; low bits are always 0
230 uint8_t tag; // Needs to be 0x10 for this report
231 struct pssense_output_settings settings;
232 uint8_t counter;
233 uint8_t haptics[PCM_HAPTIC_BUF_SIZE];
234 __le32 crc;
235};
236static_assert(sizeof(struct pssense_ps5_output_report) == PS5_OUTPUT_REPORT_LENGTH,
237 "Incorrect output report struct length");
238
239#define FEATURE_REPORT_LENGTH 64
240#define CALIBRATION_DATA_LENGTH 116
241
242/**
243 * HID output report data packet.
244 */
246{
247 uint8_t report_id;
248 uint8_t part_id;
249 uint8_t data[CALIBRATION_DATA_LENGTH / 2];
250 __le32 crc;
251};
252static_assert(sizeof(struct pssense_feature_report) == FEATURE_REPORT_LENGTH, "Incorrect feature report struct length");
253
254#pragma pack(pop)
#define MATH_GRAVITY_M_S2
Standard gravity acceleration constant.
Definition m_api.h:53
C interface to math library.
pssense_output_settings_flag2
Definition pssense_protocol.h:120
@ 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:124
const double PSSENSE_ACCEL_SCALE
Accelerometer read value range is +-32768 and covers +-8 g.
Definition pssense_protocol.h:47
const double PSSENSE_GYRO_SCALE_DEG
Gyro read value range is +-32768.
Definition pssense_protocol.h:45
HID output report data packet.
Definition pssense_protocol.h:246
HID input report data packet.
Definition pssense_protocol.h:61
Definition pssense_protocol.h:185
__le32 cycle_position
The position, in IMU ticks, NS_PER_IMU_TICK.
Definition pssense_protocol.h:190
__le32 cycle_length
The length, in thirds of a nanosecond.
Definition pssense_protocol.h:192
Definition pssense_protocol.h:157
Definition pssense_protocol.h:197
uint8_t flag2
See enum pssense_output_settings_flag2.
Definition pssense_protocol.h:201
struct pssense_led_settings led_settings
Settings of the tracking LEDs.
Definition pssense_protocol.h:211
__le32 host_timestamp_send_time_us
Time of packet send, in host time, in microseconds.
Definition pssense_protocol.h:209
uint8_t status_led_enable
Whether to enable the status LED or not.
Definition pssense_protocol.h:215
uint8_t vibration_amplitude
Vibration amplitude from 0x00-0xff. Sending 0 turns vibration off.
Definition pssense_protocol.h:203
uint8_t flag1
See enum pssense_output_settings_flag1.
Definition pssense_protocol.h:199
struct pssense_output_adaptive_trigger_settings trigger_settings
Settings for the adaptive trigger.
Definition pssense_protocol.h:207
uint8_t trigger_haptics_reduction
Lower 4 bits for haptics reduction, upper 4 bits for trigger reduction. Decreases in 12....
Definition pssense_protocol.h:213
uint8_t unk0
Sony driver sometimes sets to 0x82.
Definition pssense_protocol.h:205
HID output report data packet matching the PS5 layout, with PCM haptics.
Definition pssense_protocol.h:227
Endian-specific byte order defines.