Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp4204886rwb; Tue, 20 Sep 2022 10:13:38 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5jc7erWq12PgUcZPoLSl3RbzD7NY7ANAAltIXvggNdgFfOpj8E8/r84eHO6QGxnv9zH68W X-Received: by 2002:a63:4c17:0:b0:43b:e31c:efd0 with SMTP id z23-20020a634c17000000b0043be31cefd0mr283694pga.213.1663694017830; Tue, 20 Sep 2022 10:13:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663694017; cv=none; d=google.com; s=arc-20160816; b=vC+nah4s+dIABm/Y9h1hzxxQIa4qIwRDtMsLiSzkZ1dDUjeXxktxWlcpoWFEoV5fa/ Unuiz5NX6skEepIn8B2LD9Vfq3ErJFLjueIIq2jIq/dtjORfhODXejuV2dCjTKvt0gId UzgrwqDI62vTDqletAkIHXQajZbWuj+EGQSjTB/96AUQ1ixoEpwHqrDEmwZUfh2/72Cs TeTbvIe/saFZlxRuR5uaSqfuTdocz7vaNOVz+K2KHccadXtHOXnwS5WJ8ZqD9slMYqUd KRO3jMZcFEcrY7WIOSg9/p9FHEGdsOIWkTIhBwvLZpV0d1bsU0ZF5Ga9KuTb5KCkwNZZ 3Adg== 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=b6ndjbcZhGSJGsthJvwVEPx5dH9yn8An6taijSqj7Go=; b=QjKUbL812ORMRqfF1aDtqY3zhq6SW8Kf+mhdU4V96QZoNf53Dd5QMxDmorCdLeyStD 3uVHKcfXqlw0vv8tlXHHo8kaHPUZi41sAG91OGq+ZOtCI1mTk/RAEiMcE3JLA3etqTcW Gw9VgICeK1FiMEqYdrAqLr7Od9Ge6fganpyDvYjTZZwBImnmBG71ah6yRsU9vFyutKSf SbzDduLOyeg5ZjwmxowI5EeDH0Hx9tIwENefKqQlNnjFNvYscgxcZ5wXZ8qdFNIArDlG fbGNOlVS6VpzuUgBZKQ3Egg9k7fvXL9xqh3HBZYrM2eKQivuL3HfUpPP8vQuEEenGigD vJsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=ow6j0wF5; 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 i10-20020a1709026aca00b0017808c0aa8bsi266405plt.115.2022.09.20.10.13.25; Tue, 20 Sep 2022 10:13:37 -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=ow6j0wF5; 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 S231255AbiITQzQ (ORCPT + 99 others); Tue, 20 Sep 2022 12:55:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229631AbiITQzG (ORCPT ); Tue, 20 Sep 2022 12:55:06 -0400 Received: from mail-0201.mail-europe.com (mail-0201.mail-europe.com [51.77.79.158]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 759EE29C81 for ; Tue, 20 Sep 2022 09:54:52 -0700 (PDT) Date: Tue, 20 Sep 2022 16:54:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1663692888; x=1663952088; bh=b6ndjbcZhGSJGsthJvwVEPx5dH9yn8An6taijSqj7Go=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID; b=ow6j0wF5pGYOm+Gw463dsuCkcLJvXC8Czc3BMRj3RYX3BhJU4IP7fXy5In8SOhfIf M7QwGTOoAan0c8P8PeonOAwRpFtCt2GYpxQNOsy4xSSYXjrWYWZ5cVYIGJGZhUzuk3 6Cyvasg/xHsbCy8OTu/CRoPG9rtVDzGjDitprIR7sEwj4Ys9/GHyptQMM+FSs5P72v /QNO5miBdY5mNNPR6wL96kdqhZeB5pdI3FGo8HfhKk9Ya0o+eJZwp1zyUuVktsz6ld GlxNlom+xcT/OR4A28lCOjZv8UhC9QWhTZ78V6nYmFIcsAihVfZ2QymBnVAiTSpOQr Lov7O+BksP3Gg== To: "linux-kernel@vger.kernel.org" , "linux-input@vger.kernel.org" , Benjamin Tissoires , Jiri Kosina , "Daniel J. Ogorchock" From: Johnothan King Subject: [PATCH v2] 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 autolearn=unavailable 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 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(). 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 | 58 ++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/drivers/hid/hid-nintendo.c b/drivers/hid/hid-nintendo.c index 6028af3c3aae..ca58f5440159 100644 --- a/drivers/hid/hid-nintendo.c +++ b/drivers/hid/hid-nintendo.c @@ -760,12 +760,34 @@ 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 values are plausible */ +=09if (cal_x->min =3D=3D cal_x->center || +=09 cal_x->center =3D=3D cal_x->max || +=09 cal_y->min =3D=3D cal_y->center || +=09 cal_y->center =3D=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 joycon_ctlr *ctlr, +=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(ctlr->hdev, +=09=09 "Failed to read %s stick cal, " +=09=09 "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 +815,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); =20 -=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; - -=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, +=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, +=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