@@ -760,12 +760,31 @@ static int joycon_read_stick_calibration(struct joycon_ctlr *ctlr, u16 cal_addr,
760
760
cal_y -> max = cal_y -> center + y_max_above ;
761
761
cal_y -> min = cal_y -> center - y_min_below ;
762
762
763
- return 0 ;
763
+ /* check if calibration values are plausible */
764
+ if (cal_x -> min >= cal_x -> center || cal_x -> center >= cal_x -> max ||
765
+ cal_y -> min >= cal_y -> center || cal_y -> center >= cal_y -> max )
766
+ ret = - EINVAL ;
767
+
768
+ return ret ;
764
769
}
765
770
766
771
static const u16 DFLT_STICK_CAL_CEN = 2000 ;
767
772
static const u16 DFLT_STICK_CAL_MAX = 3500 ;
768
773
static const u16 DFLT_STICK_CAL_MIN = 500 ;
774
+ static void joycon_use_default_calibration (struct hid_device * hdev ,
775
+ struct joycon_stick_cal * cal_x ,
776
+ struct joycon_stick_cal * cal_y ,
777
+ const char * stick , int ret )
778
+ {
779
+ hid_warn (hdev ,
780
+ "Failed to read %s stick cal, using defaults; e=%d\n" ,
781
+ stick , ret );
782
+
783
+ cal_x -> center = cal_y -> center = DFLT_STICK_CAL_CEN ;
784
+ cal_x -> max = cal_y -> max = DFLT_STICK_CAL_MAX ;
785
+ cal_x -> min = cal_y -> min = DFLT_STICK_CAL_MIN ;
786
+ }
787
+
769
788
static int joycon_request_calibration (struct joycon_ctlr * ctlr )
770
789
{
771
790
u16 left_stick_addr = JC_CAL_FCT_DATA_LEFT_ADDR ;
@@ -793,38 +812,24 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)
793
812
& ctlr -> left_stick_cal_x ,
794
813
& ctlr -> left_stick_cal_y ,
795
814
true);
796
- if (ret ) {
797
- hid_warn (ctlr -> hdev ,
798
- "Failed to read left stick cal, using dflts; e=%d\n" ,
799
- ret );
800
-
801
- ctlr -> left_stick_cal_x .center = DFLT_STICK_CAL_CEN ;
802
- ctlr -> left_stick_cal_x .max = DFLT_STICK_CAL_MAX ;
803
- ctlr -> left_stick_cal_x .min = DFLT_STICK_CAL_MIN ;
804
815
805
- ctlr -> left_stick_cal_y .center = DFLT_STICK_CAL_CEN ;
806
- ctlr -> left_stick_cal_y .max = DFLT_STICK_CAL_MAX ;
807
- ctlr -> left_stick_cal_y .min = DFLT_STICK_CAL_MIN ;
808
- }
816
+ if (ret )
817
+ joycon_use_default_calibration (ctlr -> hdev ,
818
+ & ctlr -> left_stick_cal_x ,
819
+ & ctlr -> left_stick_cal_y ,
820
+ "left" , ret );
809
821
810
822
/* read the right stick calibration data */
811
823
ret = joycon_read_stick_calibration (ctlr , right_stick_addr ,
812
824
& ctlr -> right_stick_cal_x ,
813
825
& ctlr -> right_stick_cal_y ,
814
826
false);
815
- if (ret ) {
816
- hid_warn (ctlr -> hdev ,
817
- "Failed to read right stick cal, using dflts; e=%d\n" ,
818
- ret );
819
-
820
- ctlr -> right_stick_cal_x .center = DFLT_STICK_CAL_CEN ;
821
- ctlr -> right_stick_cal_x .max = DFLT_STICK_CAL_MAX ;
822
- ctlr -> right_stick_cal_x .min = DFLT_STICK_CAL_MIN ;
823
827
824
- ctlr -> right_stick_cal_y .center = DFLT_STICK_CAL_CEN ;
825
- ctlr -> right_stick_cal_y .max = DFLT_STICK_CAL_MAX ;
826
- ctlr -> right_stick_cal_y .min = DFLT_STICK_CAL_MIN ;
827
- }
828
+ if (ret )
829
+ joycon_use_default_calibration (ctlr -> hdev ,
830
+ & ctlr -> right_stick_cal_x ,
831
+ & ctlr -> right_stick_cal_y ,
832
+ "right" , ret );
828
833
829
834
hid_dbg (ctlr -> hdev , "calibration:\n"
830
835
"l_x_c=%d l_x_max=%d l_x_min=%d\n"
0 commit comments