Skip to content

Commit

Permalink
HID: nintendo: check analog user calibration for plausibility
Browse files Browse the repository at this point in the history
[ Upstream commit 50503e3 ]

Arne Wendt writes:
  Cheap clone controllers may (falsely) report as having a user
  calibration for the analog sticks in place, but return
  wrong/impossible values for the actual calibration data.
  In the present case at mine, the controller reports having a
  user calibration in place and successfully executes the read
  commands. The reported user calibration however is
  min = center = max = 0.

  This pull request addresses problems of this kind by checking the
  provided user calibration-data for plausibility (min < center < max)
  and falling back to the default values if implausible.

I'll note that I was experiencing a crash because of this bug when using
the GuliKit KingKong 2 controller. The crash manifests as a divide by
zero error in the kernel logs:
kernel: divide error: 0000 [#1] PREEMPT SMP NOPTI

Link: nicman23/dkms-hid-nintendo#25
Link: DanielOgorchock/linux#36
Co-authored-by: Arne Wendt <[email protected]>
Signed-off-by: Johnothan King <[email protected]>
Signed-off-by: Benjamin Tissoires <[email protected]>
Link: https://lore.kernel.org/r/gvpL2G6VwXGJPvxX5KRiu9pVjvTivgayug_jdKDY6zfuAaAqncP9BkKLosjwUXNlgVVTMfJSKfwPF1K79cKAkwGComyC21vCV3q9B3EXNkE=@protonmail.com
Signed-off-by: Sasha Levin <[email protected]>
  • Loading branch information
2 people authored and Sasha Levin committed Oct 21, 2022
1 parent b4455aa commit d5ff4a0
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions drivers/hid/hid-nintendo.c
Original file line number Diff line number Diff line change
Expand Up @@ -760,12 +760,31 @@ static int joycon_read_stick_calibration(struct joycon_ctlr *ctlr, u16 cal_addr,
cal_y->max = cal_y->center + y_max_above;
cal_y->min = cal_y->center - y_min_below;

return 0;
/* check if calibration values are plausible */
if (cal_x->min >= cal_x->center || cal_x->center >= cal_x->max ||
cal_y->min >= cal_y->center || cal_y->center >= cal_y->max)
ret = -EINVAL;

return ret;
}

static const u16 DFLT_STICK_CAL_CEN = 2000;
static const u16 DFLT_STICK_CAL_MAX = 3500;
static const u16 DFLT_STICK_CAL_MIN = 500;
static void joycon_use_default_calibration(struct hid_device *hdev,
struct joycon_stick_cal *cal_x,
struct joycon_stick_cal *cal_y,
const char *stick, int ret)
{
hid_warn(hdev,
"Failed to read %s stick cal, using defaults; e=%d\n",
stick, ret);

cal_x->center = cal_y->center = DFLT_STICK_CAL_CEN;
cal_x->max = cal_y->max = DFLT_STICK_CAL_MAX;
cal_x->min = cal_y->min = DFLT_STICK_CAL_MIN;
}

static int joycon_request_calibration(struct joycon_ctlr *ctlr)
{
u16 left_stick_addr = JC_CAL_FCT_DATA_LEFT_ADDR;
Expand Down Expand Up @@ -793,38 +812,24 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)
&ctlr->left_stick_cal_x,
&ctlr->left_stick_cal_y,
true);
if (ret) {
hid_warn(ctlr->hdev,
"Failed to read left stick cal, using dflts; e=%d\n",
ret);

ctlr->left_stick_cal_x.center = DFLT_STICK_CAL_CEN;
ctlr->left_stick_cal_x.max = DFLT_STICK_CAL_MAX;
ctlr->left_stick_cal_x.min = DFLT_STICK_CAL_MIN;

ctlr->left_stick_cal_y.center = DFLT_STICK_CAL_CEN;
ctlr->left_stick_cal_y.max = DFLT_STICK_CAL_MAX;
ctlr->left_stick_cal_y.min = DFLT_STICK_CAL_MIN;
}
if (ret)
joycon_use_default_calibration(ctlr->hdev,
&ctlr->left_stick_cal_x,
&ctlr->left_stick_cal_y,
"left", ret);

/* read the right stick calibration data */
ret = joycon_read_stick_calibration(ctlr, right_stick_addr,
&ctlr->right_stick_cal_x,
&ctlr->right_stick_cal_y,
false);
if (ret) {
hid_warn(ctlr->hdev,
"Failed to read right stick cal, using dflts; e=%d\n",
ret);

ctlr->right_stick_cal_x.center = DFLT_STICK_CAL_CEN;
ctlr->right_stick_cal_x.max = DFLT_STICK_CAL_MAX;
ctlr->right_stick_cal_x.min = DFLT_STICK_CAL_MIN;

ctlr->right_stick_cal_y.center = DFLT_STICK_CAL_CEN;
ctlr->right_stick_cal_y.max = DFLT_STICK_CAL_MAX;
ctlr->right_stick_cal_y.min = DFLT_STICK_CAL_MIN;
}
if (ret)
joycon_use_default_calibration(ctlr->hdev,
&ctlr->right_stick_cal_x,
&ctlr->right_stick_cal_y,
"right", ret);

hid_dbg(ctlr->hdev, "calibration:\n"
"l_x_c=%d l_x_max=%d l_x_min=%d\n"
Expand Down

0 comments on commit d5ff4a0

Please sign in to comment.