From patchwork Wed Sep 14 07:43:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johnothan King X-Patchwork-Id: 607065 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2BB60ECAAD8 for ; Wed, 14 Sep 2022 07:44:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229436AbiINHn7 (ORCPT ); Wed, 14 Sep 2022 03:43:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229484AbiINHn6 (ORCPT ); Wed, 14 Sep 2022 03:43:58 -0400 Received: from mail-0301.mail-europe.com (mail-0301.mail-europe.com [188.165.51.139]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9164F72B67 for ; Wed, 14 Sep 2022 00:43:57 -0700 (PDT) Date: Wed, 14 Sep 2022 07:43:50 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1663141433; x=1663400633; bh=HppxmTtawa0nrgXshMeDsopmQeqOWidtWPtFGQHpuZY=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID; b=o5kNx5WbyZ0y8VrZOOCtDHVvRmBYC46vP+p9gAONc/8AXyQLuEbc66VXDoAezrdTl cPMQRvQ6IXevWE6Qi6pD40TYLOHcchUiIY3MeCsWRJzjIlQVAKHcKr3HumlxVjzVbI Rq3DEr2WrKrBP5DxkmCUvF9Kc6yoBqyxXSCdlhw+fDK1u4/xAnJJjJ1ObLKxXxiLi/ Hj3Lp9nWgggSZSTvnLTDQOv3pBv7clOe7OuAeeCaqsN6TwJ0+Pgx4dxZyGDKoHs1Lt TzLUoWu+5i1IPYCFP3KwSnqOj1uYOx36EEFNphmOP6ucCbGucUfI3rjt/D3UjVJgdy qX7a0f0KL7l4g== To: "Daniel J. Ogorchock" , Jiri Kosina , Benjamin Tissoires , "linux-input@vger.kernel.org" , "linux-kernel@vger.kernel.org" From: Johnothan King Subject: [PATCH] HID: nintendo: check analog user calibration for plausibility Message-ID: Feedback-ID: 1750573:user:proton MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org 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: https://github.com/nicman23/dkms-hid-nintendo/pull/25 Link: https://github.com/DanielOgorchock/linux/issues/36 Co-authored-by: Arne Wendt Signed-off-by: Johnothan King --- drivers/hid/hid-nintendo.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/drivers/hid/hid-nintendo.c b/drivers/hid/hid-nintendo.c index 6028af3c3aae..7f287f6a75f5 100644 --- a/drivers/hid/hid-nintendo.c +++ b/drivers/hid/hid-nintendo.c @@ -793,7 +793,17 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) &ctlr->left_stick_cal_x, &ctlr->left_stick_cal_y, true); - if (ret) { + + /* + * Check whether read succeeded and perform plausibility check + * for retrieved values. + */ + if (ret || + ctlr->left_stick_cal_x.min >= ctlr->left_stick_cal_x.center || + ctlr->left_stick_cal_x.center >= ctlr->left_stick_cal_x.max || + ctlr->left_stick_cal_y.min >= ctlr->left_stick_cal_y.center || + ctlr->left_stick_cal_y.center >= ctlr->left_stick_cal_y.max + ) { hid_warn(ctlr->hdev, "Failed to read left stick cal, using dflts; e=%d\n", ret); @@ -812,7 +822,17 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr) &ctlr->right_stick_cal_x, &ctlr->right_stick_cal_y, false); - if (ret) { + + /* + * Check whether read succeeded and perform plausibility check + * for retrieved values. + */ + if (ret || + ctlr->right_stick_cal_x.min >= ctlr->right_stick_cal_x.center || + ctlr->right_stick_cal_x.center >= ctlr->right_stick_cal_x.max || + ctlr->right_stick_cal_y.min >= ctlr->right_stick_cal_y.center || + ctlr->right_stick_cal_y.center >= ctlr->right_stick_cal_y.max + ) { hid_warn(ctlr->hdev, "Failed to read right stick cal, using dflts; e=%d\n", ret);