@@ -944,6 +944,7 @@ ATTRIBUTE_GROUPS(ps_device);
944
944
945
945
static int dualsense_get_calibration_data (struct dualsense * ds )
946
946
{
947
+ struct hid_device * hdev = ds -> base .hdev ;
947
948
short gyro_pitch_bias , gyro_pitch_plus , gyro_pitch_minus ;
948
949
short gyro_yaw_bias , gyro_yaw_plus , gyro_yaw_minus ;
949
950
short gyro_roll_bias , gyro_roll_plus , gyro_roll_minus ;
@@ -954,6 +955,7 @@ static int dualsense_get_calibration_data(struct dualsense *ds)
954
955
int speed_2x ;
955
956
int range_2g ;
956
957
int ret = 0 ;
958
+ int i ;
957
959
uint8_t * buf ;
958
960
959
961
buf = kzalloc (DS_FEATURE_REPORT_CALIBRATION_SIZE , GFP_KERNEL );
@@ -1005,6 +1007,21 @@ static int dualsense_get_calibration_data(struct dualsense *ds)
1005
1007
ds -> gyro_calib_data [2 ].sens_numer = speed_2x * DS_GYRO_RES_PER_DEG_S ;
1006
1008
ds -> gyro_calib_data [2 ].sens_denom = gyro_roll_plus - gyro_roll_minus ;
1007
1009
1010
+ /*
1011
+ * Sanity check gyro calibration data. This is needed to prevent crashes
1012
+ * during report handling of virtual, clone or broken devices not implementing
1013
+ * calibration data properly.
1014
+ */
1015
+ for (i = 0 ; i < ARRAY_SIZE (ds -> gyro_calib_data ); i ++ ) {
1016
+ if (ds -> gyro_calib_data [i ].sens_denom == 0 ) {
1017
+ hid_warn (hdev , "Invalid gyro calibration data for axis (%d), disabling calibration." ,
1018
+ ds -> gyro_calib_data [i ].abs_code );
1019
+ ds -> gyro_calib_data [i ].bias = 0 ;
1020
+ ds -> gyro_calib_data [i ].sens_numer = DS_GYRO_RANGE ;
1021
+ ds -> gyro_calib_data [i ].sens_denom = S16_MAX ;
1022
+ }
1023
+ }
1024
+
1008
1025
/*
1009
1026
* Set accelerometer calibration and normalization parameters.
1010
1027
* Data values will be normalized to 1/DS_ACC_RES_PER_G g.
@@ -1027,6 +1044,21 @@ static int dualsense_get_calibration_data(struct dualsense *ds)
1027
1044
ds -> accel_calib_data [2 ].sens_numer = 2 * DS_ACC_RES_PER_G ;
1028
1045
ds -> accel_calib_data [2 ].sens_denom = range_2g ;
1029
1046
1047
+ /*
1048
+ * Sanity check accelerometer calibration data. This is needed to prevent crashes
1049
+ * during report handling of virtual, clone or broken devices not implementing calibration
1050
+ * data properly.
1051
+ */
1052
+ for (i = 0 ; i < ARRAY_SIZE (ds -> accel_calib_data ); i ++ ) {
1053
+ if (ds -> accel_calib_data [i ].sens_denom == 0 ) {
1054
+ hid_warn (hdev , "Invalid accelerometer calibration data for axis (%d), disabling calibration." ,
1055
+ ds -> accel_calib_data [i ].abs_code );
1056
+ ds -> accel_calib_data [i ].bias = 0 ;
1057
+ ds -> accel_calib_data [i ].sens_numer = DS_ACC_RANGE ;
1058
+ ds -> accel_calib_data [i ].sens_denom = S16_MAX ;
1059
+ }
1060
+ }
1061
+
1030
1062
err_free :
1031
1063
kfree (buf );
1032
1064
return ret ;
0 commit comments