@@ -760,12 +760,31 @@ static int joycon_read_stick_calibration(struct joycon_ctlr *ctlr, u16 cal_addr,
760760 cal_y -> max = cal_y -> center + y_max_above ;
761761 cal_y -> min = cal_y -> center - y_min_below ;
762762
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 ;
764769}
765770
766771static const u16 DFLT_STICK_CAL_CEN = 2000 ;
767772static const u16 DFLT_STICK_CAL_MAX = 3500 ;
768773static 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+
769788static int joycon_request_calibration (struct joycon_ctlr * ctlr )
770789{
771790 u16 left_stick_addr = JC_CAL_FCT_DATA_LEFT_ADDR ;
@@ -793,38 +812,24 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)
793812 & ctlr -> left_stick_cal_x ,
794813 & ctlr -> left_stick_cal_y ,
795814 true);
796- if (ret ) {
797- hid_warn (ctlr -> hdev ,
798- "Failed to read left stick cal, using dflts; e=%d\n" ,
799- ret );
800815
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-
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 );
809821
810822 /* read the right stick calibration data */
811823 ret = joycon_read_stick_calibration (ctlr , right_stick_addr ,
812824 & ctlr -> right_stick_cal_x ,
813825 & ctlr -> right_stick_cal_y ,
814826 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 ;
823827
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 );
828833
829834 hid_dbg (ctlr -> hdev , "calibration:\n"
830835 "l_x_c=%d l_x_max=%d l_x_min=%d\n"
@@ -1904,9 +1909,8 @@ static int joycon_leds_create(struct joycon_ctlr *ctlr)
19041909 /* Set the home LED to 0 as default state */
19051910 ret = joycon_home_led_brightness_set (led , 0 );
19061911 if (ret ) {
1907- hid_err (hdev , "Failed to set home LED dflt; ret=%d\n" ,
1908- ret );
1909- return ret ;
1912+ hid_warn (hdev , "Failed to set home LED default, unregistering home LED" );
1913+ devm_led_classdev_unregister (& hdev -> dev , led );
19101914 }
19111915 }
19121916
0 commit comments