Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp7591560rwn; Wed, 14 Sep 2022 00:52:05 -0700 (PDT) X-Google-Smtp-Source: AA6agR5VVdE+WTQ5zkSACw8y6CMhUMzEFhdQ3G/an4elJ9WrqDAxrhwl9oXbhZ2sG6hhYkP8AfU0 X-Received: by 2002:a63:2541:0:b0:434:af9b:252d with SMTP id l62-20020a632541000000b00434af9b252dmr29974096pgl.585.1663141925545; Wed, 14 Sep 2022 00:52:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663141925; cv=none; d=google.com; s=arc-20160816; b=on4r9otPNkenSKdjU/0ZwatYulpnDfDQgSCqM+W5Lk1u1jYCb/Zp2qDMbMgs+EQcSI InzVZWKzKOc2lvy0oPcLBtnW0qjwx6oN75Gm28+oT2gCcXujFVu2ykq/Y+2pIphWgnYZ CmWb6K3XMxIz5jKrJbGH+a1hdL6G7qvVwHXsQ1Mu01ZgQZFw7v92yv+4mrXHwzoSvG5g a2vbW0QjqSNzCqYl98Jenwpy44pT36wcdmAUDW8g20TiC5stt71twDpD2Q36cdlzHWr2 Gfu3UI7JDxmvjp/cbpPZSQ4p+jXKSB6Ejzr61ZZv2fOVmk7vqZ5l7ZnmjUmP5EMkRo2S 3/iA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:message-id:subject:from:to:dkim-signature:date; bh=HppxmTtawa0nrgXshMeDsopmQeqOWidtWPtFGQHpuZY=; b=yGRY9H1RZSQz32Q0Hi69W9FnP6i+G1a6RE/fip1/sYaBKnkZZ8Cq25VcFTyvgzE2xz XCuYINGT9iNRCMn1WejZTh7X+Rhn675KNs0ccrDMQdd+gpKfLS0ajFx939xKEbmYKZqf eH1hPGPQEa7qh9P2XRr1cmdJxys2ErUVs2gCoGcOW2we9dilR65SFdv8bO9EgFD1YHte XG/+5/VD864f0cel97dOBEUszvYOTmBufhIkkzO6SfDzphR1re5mIzVMhxHXkB6lnLZB wrhKN+J1o3vdLuB90G8aC+WSiNixsVkjJjfwgSydBUcO6pZToVG6cEvAbZaiwwhf+jI1 g/Pw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=o5kNx5Wb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z14-20020a170902ccce00b001782bd6c443si9563701ple.621.2022.09.14.00.51.53; Wed, 14 Sep 2022 00:52:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=o5kNx5Wb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229925AbiINHoA (ORCPT + 99 others); Wed, 14 Sep 2022 03:44:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229890AbiINHn6 (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 90EA172B64 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 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 =3D center =3D max =3D 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_ct= lr *ctlr) =09=09=09=09=09 &ctlr->left_stick_cal_x, =09=09=09=09=09 &ctlr->left_stick_cal_y, =09=09=09=09=09 true); -=09if (ret) { + +=09/* +=09 * Check whether read succeeded and perform plausibility check +=09 * for retrieved values. +=09 */ +=09if (ret || +=09=09ctlr->left_stick_cal_x.min >=3D ctlr->left_stick_cal_x.center || +=09=09ctlr->left_stick_cal_x.center >=3D ctlr->left_stick_cal_x.max || +=09=09ctlr->left_stick_cal_y.min >=3D ctlr->left_stick_cal_y.center || +=09=09ctlr->left_stick_cal_y.center >=3D ctlr->left_stick_cal_y.max +=09) { =09=09hid_warn(ctlr->hdev, =09=09=09 "Failed to read left stick cal, using dflts; e=3D%d\n", =09=09=09 ret); @@ -812,7 +822,17 @@ static int joycon_request_calibration(struct joycon_ct= lr *ctlr) =09=09=09=09=09 &ctlr->right_stick_cal_x, =09=09=09=09=09 &ctlr->right_stick_cal_y, =09=09=09=09=09 false); -=09if (ret) { + +=09/* +=09 * Check whether read succeeded and perform plausibility check +=09 * for retrieved values. +=09 */ +=09if (ret || +=09=09ctlr->right_stick_cal_x.min >=3D ctlr->right_stick_cal_x.center || +=09=09ctlr->right_stick_cal_x.center >=3D ctlr->right_stick_cal_x.max || +=09=09ctlr->right_stick_cal_y.min >=3D ctlr->right_stick_cal_y.center || +=09=09ctlr->right_stick_cal_y.center >=3D ctlr->right_stick_cal_y.max +=09) { =09=09hid_warn(ctlr->hdev, =09=09=09 "Failed to read right stick cal, using dflts; e=3D%d\n", =09=09=09 ret); --=20 2.37.3