Re: [PATCH v2] HID: nintendo: check analog user calibration for plausibility

From: Daniel Ogorchock
Date: Tue Sep 20 2022 - 18:02:53 EST


On Tue, Sep 20, 2022 at 12:54 PM Johnothan King
<johnothanking@xxxxxxxxxxxxxx> wrote:
>
> 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
>
> Changes in v2:
> - Move the plausibility check to joycon_read_stick_calibration() and
> have that function return -EINVAL if the check fails.
> - In the plausibility check, change >= to ==. hid_field_extract() never
> returns a negative value, so a scenario involving min > center or
> center > max is impossible.
> - To reduce code duplication, move the code for setting default
> calibration values into a single function called
> joycon_use_default_calibration().
>
> Link: https://github.com/nicman23/dkms-hid-nintendo/pull/25
> Link: https://github.com/DanielOgorchock/linux/issues/36
> Co-authored-by: Arne Wendt <arne.wendt@xxxxxxx>
> Signed-off-by: Johnothan King <johnothanking@xxxxxxxxxxxxxx>
> ---
> drivers/hid/hid-nintendo.c | 58 ++++++++++++++++++++++----------------
> 1 file changed, 33 insertions(+), 25 deletions(-)
>

Reviewed-by: Daniel J. Ogorchock <djogorchock@xxxxxxxxx>

Thanks for the fix. This seems like a good way around invalid calibration data.

-Daniel