Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp5176603rwb; Wed, 21 Sep 2022 04:23:45 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6DJUCj22cRCTPTIBeGPsYaCUGN5F90/2vM7gE2I/gQa2lp0h73ttmxA7h0feWppmzT996r X-Received: by 2002:a17:907:805:b0:782:1a0d:337f with SMTP id wv5-20020a170907080500b007821a0d337fmr1529820ejb.475.1663759424776; Wed, 21 Sep 2022 04:23:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663759424; cv=none; d=google.com; s=arc-20160816; b=0/nz3z1IMndmQMbrDgX6SiPM7O37c4GqoLPNBq+74vnDSvkSfLMMbKAL7PO3W5umi3 us+QBoYWyl7d03jv7XUWCgmFPhSZ2DOqZktW8Qn9rMXRteqjGoneId+5oDkh9gIMG9pY gbFKmTem0DUQUQhUYXuIA/HsU9vKAiSnfXDRLOUx0NuG8T7RG/Q/VFkbd05evfLZ9CmF 8dxjgKnlq/mlQCt0TsoZG8FboaIodz5b64iJzJ/Md5cbp9PGKWPU5CQOYViFXflRA3C4 gqurCbGP9VDp1cAiaBTFQjvc7wXoufuJbx64XzMhKwVCXnUOxfWk+vTbMbKVBeETS57f 1e+A== 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=ghRWy/e90VWRHHDbxBAs0t0Nb9qsu1i8ost31cToLGU=; b=bvFjBUNDaUXLHvuSIgFGgFCLYKP5nQ/cbBrxVmA/8zK1fiLT2+DvWvOBGiNdRODFHM 3G/zM22xP4oXD/wn3gkPcuWAOnth13ipBNbYg9TfhHGR1Yg6ZoHOz7ZJsD4agR5lo1N6 AwNt6elb9/TPVIGMnAHBpQfsVYkYQlyPmBuwKq9E4T9OHh5Hf7+c0hDteuuT0NfWKdIT iccIT1xJ5JRFDz/aDonf5erq6NltaukyaY3O0Q0yL69aJ2lS4w/zLFby0IUgmmjG5zBP 5zmyUGtw8b1X2BNCSPD+KEBywIyY68D8IT5EUohyFssSzs9o6+yZQsK6SVbLOgLYjq3t EQ7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=KqV05N02; 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 gb31-20020a170907961f00b0077f7dd4ee9dsi25238ejc.334.2022.09.21.04.23.17; Wed, 21 Sep 2022 04:23:44 -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=KqV05N02; 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 S229980AbiIUK4L (ORCPT + 99 others); Wed, 21 Sep 2022 06:56:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229936AbiIUK4J (ORCPT ); Wed, 21 Sep 2022 06:56:09 -0400 Received: from mail-4319.protonmail.ch (mail-4319.protonmail.ch [185.70.43.19]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12FD190C57 for ; Wed, 21 Sep 2022 03:56:08 -0700 (PDT) Date: Wed, 21 Sep 2022 10:55:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1663757767; x=1664016967; bh=ghRWy/e90VWRHHDbxBAs0t0Nb9qsu1i8ost31cToLGU=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID; b=KqV05N021s1uRIPAZb0hxE5nLPPfhlVtsnoRQifU415XatpOHiU8V8Vwv4h8dRG8t ac9tWNmeMG6i1omV/8iBBIkGznTVT9mTEJbdyURMqM1kr+pRpOyNJqQleD8bVJ3aLn GfZlZtzAIs+lFwJWMbY+pf1sATzFrBGWLekv0EwUKpdTY3zSIWS1hYS4dXA0RWLVtD LVXPQ8KFrIVU8PPTvWhw1QeRdoGhH0UWBhaIsEPTv5lu8j7iDP/4j7gj/hBFI2kPs9 +SfCLu2Z5TeTdNF+H2CBGlLzFhRgp3Od1ijdda5UeYaa5/R4vGa+h3jgXSPbmjBL4d bPLYmoKZSoAtA== To: "Daniel J. Ogorchock" , Jiri Kosina , Benjamin Tissoires , "linux-input@vger.kernel.org" , "linux-kernel@vger.kernel.org" From: Johnothan King Subject: [PATCH v4] 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_PASS, SPF_PASS 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 --- 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 >=3D to =3D=3D. hid_field_extract() ne= ver 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(). Changes in v3: - Unbreak warning string to conform to coding style. - Change joycon_use_default_calibration() to accept a struct hid_device pointer instead of a struct joycon_ctlr pointer. Changes in v4: - Reformat joycon_use_default_calibration() another time to move stick and ret onto the same line. - Revert =3D=3D to >=3D change to account for rollover. - Additional minor change to comment for value check. drivers/hid/hid-nintendo.c | 55 +++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/drivers/hid/hid-nintendo.c b/drivers/hid/hid-nintendo.c index 5cb5a1d53b71..71e2a9a0882a 100644 --- a/drivers/hid/hid-nintendo.c +++ b/drivers/hid/hid-nintendo.c @@ -760,12 +760,31 @@ static int joycon_read_stick_calibration(struct joyco= n_ctlr *ctlr, u16 cal_addr, =09cal_y->max =3D cal_y->center + y_max_above; =09cal_y->min =3D cal_y->center - y_min_below; =20 -=09return 0; +=09/* check if calibration values are plausible */ +=09if (cal_x->min >=3D cal_x->center || cal_x->center >=3D cal_x->max || +=09 cal_y->min >=3D cal_y->center || cal_y->center >=3D cal_y->max) +=09=09ret =3D -EINVAL; + +=09return ret; } =20 static const u16 DFLT_STICK_CAL_CEN =3D 2000; static const u16 DFLT_STICK_CAL_MAX =3D 3500; static const u16 DFLT_STICK_CAL_MIN =3D 500; +static void joycon_use_default_calibration(struct hid_device *hdev, +=09=09=09=09=09 struct joycon_stick_cal *cal_x, +=09=09=09=09=09 struct joycon_stick_cal *cal_y, +=09=09=09=09=09 const char *stick, int ret) +{ +=09hid_warn(hdev, +=09=09 "Failed to read %s stick cal, using defaults; e=3D%d\n", +=09=09 stick, ret); + +=09cal_x->center =3D cal_y->center =3D DFLT_STICK_CAL_CEN; +=09cal_x->max =3D cal_y->max =3D DFLT_STICK_CAL_MAX; +=09cal_x->min =3D cal_y->min =3D DFLT_STICK_CAL_MIN; +} + static int joycon_request_calibration(struct joycon_ctlr *ctlr) { =09u16 left_stick_addr =3D JC_CAL_FCT_DATA_LEFT_ADDR; @@ -793,38 +812,24 @@ static int joycon_request_calibration(struct joycon_c= tlr *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=09hid_warn(ctlr->hdev, -=09=09=09 "Failed to read left stick cal, using dflts; e=3D%d\n", -=09=09=09 ret); - -=09=09ctlr->left_stick_cal_x.center =3D DFLT_STICK_CAL_CEN; -=09=09ctlr->left_stick_cal_x.max =3D DFLT_STICK_CAL_MAX; -=09=09ctlr->left_stick_cal_x.min =3D DFLT_STICK_CAL_MIN; =20 -=09=09ctlr->left_stick_cal_y.center =3D DFLT_STICK_CAL_CEN; -=09=09ctlr->left_stick_cal_y.max =3D DFLT_STICK_CAL_MAX; -=09=09ctlr->left_stick_cal_y.min =3D DFLT_STICK_CAL_MIN; -=09} +=09if (ret) +=09=09joycon_use_default_calibration(ctlr->hdev, +=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 "left", ret); =20 =09/* read the right stick calibration data */ =09ret =3D joycon_read_stick_calibration(ctlr, right_stick_addr, =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=09hid_warn(ctlr->hdev, -=09=09=09 "Failed to read right stick cal, using dflts; e=3D%d\n", -=09=09=09 ret); - -=09=09ctlr->right_stick_cal_x.center =3D DFLT_STICK_CAL_CEN; -=09=09ctlr->right_stick_cal_x.max =3D DFLT_STICK_CAL_MAX; -=09=09ctlr->right_stick_cal_x.min =3D DFLT_STICK_CAL_MIN; =20 -=09=09ctlr->right_stick_cal_y.center =3D DFLT_STICK_CAL_CEN; -=09=09ctlr->right_stick_cal_y.max =3D DFLT_STICK_CAL_MAX; -=09=09ctlr->right_stick_cal_y.min =3D DFLT_STICK_CAL_MIN; -=09} +=09if (ret) +=09=09joycon_use_default_calibration(ctlr->hdev, +=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 "right", ret); =20 =09hid_dbg(ctlr->hdev, "calibration:\n" =09=09=09 "l_x_c=3D%d l_x_max=3D%d l_x_min=3D%d\n" --=20 2.37.3