Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp4661600rwb; Tue, 20 Sep 2022 18:07:52 -0700 (PDT) X-Google-Smtp-Source: AMsMyM693vhGmB+8KX2fCfddkcrVfIUnV6ZUfKwICqk0e0UU0/hXYEQpUZdDByYZ+FCOdwoIvHZ8 X-Received: by 2002:aa7:864a:0:b0:53f:dcdf:4614 with SMTP id a10-20020aa7864a000000b0053fdcdf4614mr26194348pfo.38.1663722472612; Tue, 20 Sep 2022 18:07:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663722472; cv=none; d=google.com; s=arc-20160816; b=Cbu52SKS2gjP+g2OXiyc5FkzBgWY56VQ1GBJwL4fbovgG+xIxH0bc/J1r2uf0GYcio nDWjCrf68hSlIq+2CsaqN9sAtNmqTQWKO7wwyPIkgytY1pckI1s5F8QVcZ9Se5Djs+Hc OIx1rbkdwinN4us6t93Vpk6+krbsx30nNeZd7G7SN3UK9LzhDnW/LBWCKeh123g8Yte5 wZyI7bSVXPMqW8Eb1c6MGxZ9s27VROQz88zA2zOpEv5Ay+L2Fy/rusb0sk+XnxY/Amkk S0MlzdfVgkGDvqMwTJHI6soeshDcCG+DzFbY+rp83mMeW6TiZbhidpDW19t7mk/c9Fzj nDRg== 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=n+ybg5BjOB6+nup0y9HKg9l5K+4YNo7fACmMKKW3RhQ=; b=m4McH8lwH7JXlhQeHtkJswAPD4N96VminSDSdrWFA13NE+5SndlKolFYtzyKEvmG0N xgVpVcXBD0Hp78h3CHcgccoKji++5jHMnC1XwEdt0Fz2N5C2mV/eCAZL//MfZLS1ayXb HCTm/j3ZdI7yc5WF1g92BkAM4mKDJK9c0wgFTlq9l33GTWFXX1Y3iHrA2mdEodxaq5jw pVmmnEtwEflGq/ABZDjJ7CXqmLAALLsjfLYECidO3Nkf5Uc6fpGh5mX95kiAS1Y2rfmi b0zzYddFClhR0VbhOlkfQyfw9mccdQ3n+ETFiyy1oR+TwdwfMXZWdK9RUqFKpkdw9WNq Y2Cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=oWKto79P; 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 nv12-20020a17090b1b4c00b001faf9ba369fsi1582185pjb.164.2022.09.20.18.07.39; Tue, 20 Sep 2022 18:07:52 -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=oWKto79P; 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 S230237AbiIUAv5 (ORCPT + 99 others); Tue, 20 Sep 2022 20:51:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229736AbiIUAvz (ORCPT ); Tue, 20 Sep 2022 20:51:55 -0400 X-Greylist: delayed 28678 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Tue, 20 Sep 2022 17:51:53 PDT Received: from mail-4318.protonmail.ch (mail-4318.protonmail.ch [185.70.43.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C76EF79620 for ; Tue, 20 Sep 2022 17:51:53 -0700 (PDT) Date: Wed, 21 Sep 2022 00:51:39 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1663721511; x=1663980711; bh=n+ybg5BjOB6+nup0y9HKg9l5K+4YNo7fACmMKKW3RhQ=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID; b=oWKto79PUCwIODCp6AlWfU4K/U4Iw6Yjdl/7A2PL9chdo7zBGDiv6hkb09PcH20B+ 22KmvEujIkz9pqtDzTAwpgZny1mxiVeSnyuVX3DociBtkzTppL0X+jbQDbFq0OmIjH g5PKkYAIfa0H0JIqg4QUrA2Wt9C82QHIg/532LCZyO2oGft80wQx/DszZUOHoffsUv xImUIFt7qDoj7rah/BHjxxuUFWYa5ULz1eQEzIpmhZ9UhGzFkZbZo5omTxqLIm0dRl y/ZpT7gcrJgPskSzu3T+VUbq77vElv28mmJhSAd1Ot72gPPHePg10zNGFWhADLFPv4 f96lHCGdVB+Ig== To: "Daniel J. Ogorchock" , Jiri Kosina , Benjamin Tissoires , "linux-input@vger.kernel.org" , "linux-kernel@vger.kernel.org" From: Johnothan King Subject: [PATCH v3] 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,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,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(). 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. 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 | 55 +++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 25 deletions(-) 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 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 || cal_x->center =3D=3D cal_x->max = || +=09 cal_y->min =3D=3D cal_y->center || 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 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", stick, +=09=09 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