Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp5170798rwb; Wed, 21 Sep 2022 04:17:55 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5fbpixB29kKK4rUylZSDu8X5tpeLEcbuWcPoahoxkJRVoCSj2JNK9kHc2Gyhk+Xgk2T+A8 X-Received: by 2002:a17:90b:4f86:b0:203:bbe8:137c with SMTP id qe6-20020a17090b4f8600b00203bbe8137cmr5070036pjb.47.1663759075436; Wed, 21 Sep 2022 04:17:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663759075; cv=none; d=google.com; s=arc-20160816; b=gl7ompjRd44OVRagEhIANyM8Q+CL1FDUdRF8F4cXQ7j7ZP58NxSAtWi+pt5RlZB8kc q0fhW0QFB+iO2T68FkVCwq6mhsEZo0OVwsJVffcjtFx1OGFqnYGzR00Jk9Q2RRCkTL45 zVSAILSQLVvx0c9t+w3be5/OsmXtiP7mQ1sE6qxLuKG3pPLz2liT4aXBUmHUfDJKCSZK z9aVcAmVKWtfx8o4NkTxljTA1tU5nCykPRx5GKt/mWuoHUtlamZgqkvjJWOBG9RYwjBS v1ac89DK5RmLZ/tqIftxy92BTzdEAmv2FNTuqa6BgXbFAHI7G7wVlh0s4feI8eyEBOTG tWjQ== 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:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=ap7mj8/mdNh1PBx7qgvwFOObSxfesy6GqXAIE71kjo4=; b=0eHFfegdCe2NY8/lb2K1UK+9iNQ3kDhRcZ6aYDO1M0+CK67h9he7V1XFKPC+qvrign K6vfeCmKTFEOJcx4NBp/UY5G/8LPkZ4uv9r/3Cd2+vApB5hece/eLzIhsER8N3G6ZQ28 547iCntugmd5MChmVumlVizX8xWVKuApSfUsuxuldnJdbSfmBhXkrU6Bo27x90FsunJa xt9MZr26QemMIR3rLU+3KZoDk/GfiQJlZBWNbNI9rsaU1sSPdHhaxQ/3PFzg3FJDXpKu baOzZ0CD77oeVKIXVDPEc3Or++XfNjvOjsnpsUz5UwcyghklTqapdw0zFsoxAZNqJAb7 hQVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=qeNCQon8; 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 62-20020a630241000000b00439f012ca7asi2740023pgc.743.2022.09.21.04.17.43; Wed, 21 Sep 2022 04:17:55 -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=qeNCQon8; 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 S229716AbiIUKxP (ORCPT + 99 others); Wed, 21 Sep 2022 06:53:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229709AbiIUKxN (ORCPT ); Wed, 21 Sep 2022 06:53:13 -0400 Received: from mail-40130.protonmail.ch (mail-40130.protonmail.ch [185.70.40.130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 289C47CAA0 for ; Wed, 21 Sep 2022 03:53:12 -0700 (PDT) Date: Wed, 21 Sep 2022 10:52:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1663757589; x=1664016789; bh=ap7mj8/mdNh1PBx7qgvwFOObSxfesy6GqXAIE71kjo4=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID; b=qeNCQon8/g6wmT455Pw90zt9ZiaMgYwtemgb2L6Z5ryCdXIyQszQIhzrfdPFJcT+W J0XwZvgj5rky25UbJbbmMvvLQJhLH9nKZ2oLH9eppVAElgCDSglMOqyQyWczLEUNmy Zxndxe5oXxBob4XnPOb013q6zqVk/xOFdrGkMSkySsNzcDkvH8yvXMhHBA2PswRjJU a1rdcsSwQpn2aw4LP0aRnZp228eyZn1KamlNYpCx2Wsz0ZaS5P9mIl1osHsgt+7lPi 2RnmVpfW28NzgOYuGdrodbGowNJJQmVpmd//O9Oi93rHvRFo5Q/ZvUkjgCI0FO55Vd 5WAfug4DRjgPg== To: Benjamin Tissoires From: Johnothan King Cc: "Daniel J. Ogorchock" , Jiri Kosina , "linux-input@vger.kernel.org" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH v3] HID: nintendo: check analog user calibration for plausibility Message-ID: In-Reply-To: <20220921083420.sm72vrsemaldjsz2@mail.corp.redhat.com> References: <20220921083420.sm72vrsemaldjsz2@mail.corp.redhat.com> 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 For my v4 patch I'll just change the check back to using >=3D. The signedness of the min, max and center values will probably need some sort of fix, but that's out of the scope of this patch. - Johnothan King ------- Original Message ------- On Wednesday, September 21st, 2022 at 1:34 AM, Benjamin Tissoires wrote: > Hi Johnothan, >=20 > On Sep 21 2022, Johnothan King wrote: >=20 > > 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. > >=20 > > 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. > >=20 > > I'll note that I was experiencing a crash because of this bug when usin= g > > 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 > >=20 > > 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()= never > > returns a negative value, so a scenario involving min > center or > > center > max is impossible. >=20 >=20 > I am not so sure this is a great idea. I agree this is correct, but it > definitely requires some processing from my brain and double > verifications in the code that this is correct. >=20 > The problem is that all of the values are declared as s32. > hid_field_extract() returns a u32, yes, but I haven't checked the report > descriptor if that value can be a negative one. What needs to be done, > if the logical min value is negative is that we should call hid_snto32() > to convert into a proper s32 (I doubt you have to do it but I am putting > it here for completeness). >=20 > So basically, you are blindly converting a u32 into a s32 and do not > take rollover into account. >=20 > Given that this function is only called at probe time where timing is > not the biggest of our concerns, I would simply leave the more human > friendy with obvious failures cases with >=3D and <=3D. >=20 >=20 >=20 > Second note: please move all "Changes in v*" below the first '---' and > before the file stats. This way they will be stripped out when applying > the patch. People who want to see the changes can always follow the lore > link that should be applied to the commit when this patch gets applied. >=20 > > - To reduce code duplication, move the code for setting default > > calibration values into a single function called > > joycon_use_default_calibration(). > >=20 > > 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. > >=20 > > 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@tuhh.de > > Signed-off-by: Johnothan King johnothanking@protonmail.com > > --- > > drivers/hid/hid-nintendo.c | 55 +++++++++++++++++++++----------------- > > 1 file changed, 30 insertions(+), 25 deletions(-) > >=20 > > diff --git a/drivers/hid/hid-nintendo.c b/drivers/hid/hid-nintendo.c > > index 6028af3c3aae..f25b7b19e9a4 100644 > > --- a/drivers/hid/hid-nintendo.c > > +++ b/drivers/hid/hid-nintendo.c > > @@ -760,12 +760,31 @@ static int joycon_read_stick_calibration(struct j= oycon_ctlr *ctlr, u16 cal_addr, > > cal_y->max =3D cal_y->center + y_max_above; > > cal_y->min =3D cal_y->center - y_min_below; > >=20 > > - return 0; > > + /* check if values are plausible */ > > + if (cal_x->min =3D=3D cal_x->center || cal_x->center =3D=3D cal_x->ma= x || > > + cal_y->min =3D=3D cal_y->center || cal_y->center =3D=3D cal_y->max) > > + ret =3D -EINVAL; > > + > > + return 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, > > + struct joycon_stick_cal *cal_x, > > + struct joycon_stick_cal *cal_y, > > + const char *stick, int ret) > > +{ > > + hid_warn(hdev, > > + "Failed to read %s stick cal, using defaults; e=3D%d\n", stick, > > + ret); >=20 >=20 > nitpick: why not putting the format string on the line above and leave > "stick" and "ret in the second line? It should be OK for checkpatch and > will be less weird to have "ret" on its line all by itself. >=20 > > + > > + cal_x->center =3D cal_y->center =3D DFLT_STICK_CAL_CEN; > > + cal_x->max =3D cal_y->max =3D DFLT_STICK_CAL_MAX; > > + cal_x->min =3D cal_y->min =3D DFLT_STICK_CAL_MIN; > > +} > > + > > static int joycon_request_calibration(struct joycon_ctlr *ctlr) > > { > > u16 left_stick_addr =3D JC_CAL_FCT_DATA_LEFT_ADDR; > > @@ -793,38 +812,24 @@ static int joycon_request_calibration(struct joyc= on_ctlr *ctlr) > > &ctlr->left_stick_cal_x, > > &ctlr->left_stick_cal_y, > > true); > > - if (ret) { > > - hid_warn(ctlr->hdev, > > - "Failed to read left stick cal, using dflts; e=3D%d\n", > > - ret); > > - > > - ctlr->left_stick_cal_x.center =3D DFLT_STICK_CAL_CEN; > > - ctlr->left_stick_cal_x.max =3D DFLT_STICK_CAL_MAX; > > - ctlr->left_stick_cal_x.min =3D DFLT_STICK_CAL_MIN; > >=20 > > - ctlr->left_stick_cal_y.center =3D DFLT_STICK_CAL_CEN; > > - ctlr->left_stick_cal_y.max =3D DFLT_STICK_CAL_MAX; > > - ctlr->left_stick_cal_y.min =3D DFLT_STICK_CAL_MIN; > > - } > > + if (ret) > > + joycon_use_default_calibration(ctlr->hdev, > > + &ctlr->left_stick_cal_x, > > + &ctlr->left_stick_cal_y, > > + "left", ret); > >=20 > > /* read the right stick calibration data */ > > ret =3D joycon_read_stick_calibration(ctlr, right_stick_addr, > > &ctlr->right_stick_cal_x, > > &ctlr->right_stick_cal_y, > > false); > > - if (ret) { > > - hid_warn(ctlr->hdev, > > - "Failed to read right stick cal, using dflts; e=3D%d\n", > > - ret); > > - > > - ctlr->right_stick_cal_x.center =3D DFLT_STICK_CAL_CEN; > > - ctlr->right_stick_cal_x.max =3D DFLT_STICK_CAL_MAX; > > - ctlr->right_stick_cal_x.min =3D DFLT_STICK_CAL_MIN; > >=20 > > - ctlr->right_stick_cal_y.center =3D DFLT_STICK_CAL_CEN; > > - ctlr->right_stick_cal_y.max =3D DFLT_STICK_CAL_MAX; > > - ctlr->right_stick_cal_y.min =3D DFLT_STICK_CAL_MIN; > > - } > > + if (ret) > > + joycon_use_default_calibration(ctlr->hdev, > > + &ctlr->right_stick_cal_x, > > + &ctlr->right_stick_cal_y, > > + "right", ret); > >=20 > > hid_dbg(ctlr->hdev, "calibration:\n" > > "l_x_c=3D%d l_x_max=3D%d l_x_min=3D%d\n" > > -- > > 2.37.3 >=20 >=20 > Cheers, > Benjamin