Skip to content

Commit dc09688

Browse files
committed
Merge branch 'for-6.1/nintendo' into for-next
2 parents 0c33ef4 + 50503e3 commit dc09688

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

drivers/hid/hid-nintendo.c

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

766771
static const u16 DFLT_STICK_CAL_CEN = 2000;
767772
static const u16 DFLT_STICK_CAL_MAX = 3500;
768773
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+
769788
static 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);
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;
804815

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"

0 commit comments

Comments
 (0)