Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp166400rwi; Wed, 12 Oct 2022 17:58:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5kKqO1QzQRT6MzoOOrXwkvVg3rgiSuoSiBAeDFBijIyGnuoBBQM/TR2CxwEz/g/6lke9CY X-Received: by 2002:a17:90a:b103:b0:20d:69aa:a350 with SMTP id z3-20020a17090ab10300b0020d69aaa350mr7967274pjq.178.1665622730833; Wed, 12 Oct 2022 17:58:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665622730; cv=none; d=google.com; s=arc-20160816; b=moFg00qJNyWgMreFcHYll/TfRAc7DESCB0UFHFx1Nwew1N6TliaClfr/5uToc9ucP5 MI5bbovPvw884t/EG+bVG1iKwoxadE8Hp8BriJomxpYNR8biRPnDXLPECL/HW8ZNEKFN /kNYRC6ofXPl1gC/RI4gGb/ixEpM4d3SBlvgWBCLXt6mfz6wlE7fYeWTpNf/ms4X+z0S +hwgoeqVeQ/TqoqhK/Oii4NyusPqotNpvNIJQP7OymwVALecwRsSv5xnRJzXGhIvpNMi qkOaiRP10V/8OciUb9K4uf7wsBATOs/HgqStV0WHLgj2Cpol4Plvuq33OYsDxtA45Z7z znAg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=QYafK7qP6iavHRZ+xlgwUz24cYhwPP0B/8fqD2tcwWA=; b=Q8c/HOu/1gJuQrRNPw4d1AM5LfTt2f/rFc5s8mLE5Bsvu7tsGoTaiZ9g+4+tRNAXsP m12RY7cYOCOrRPvFRzQBJkm/W/+Y2T8MkCpYzJKnzSPtSbY1GL1opaeDlLvcWu8/dhza KMTYqwSk1qJFUBDEe6z1UnWQ5XG+1fSiFDYS/DbJdTQAiOUKwOlHMYDyHC1+KUNJL02q YYPA/I1SLFOA4oyhU8500h26BGxrOH1SP4+wLMy6Arfm+vOWtDPmH0v8W3PIcFcTCkFG uApBfT8GOHZpJeVBgSLehUwhG3ePnGJF504eVEOWNwbFq6zsKMA8ldo6FIHY4/j7/nQy oT5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jLVfH4qQ; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a8-20020a63e848000000b0043945685064si20492842pgk.26.2022.10.12.17.58.34; Wed, 12 Oct 2022 17:58:50 -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=@kernel.org header.s=k20201202 header.b=jLVfH4qQ; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230000AbiJMAVK (ORCPT + 99 others); Wed, 12 Oct 2022 20:21:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230162AbiJMAUB (ORCPT ); Wed, 12 Oct 2022 20:20:01 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF12315048B; Wed, 12 Oct 2022 17:17:32 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1214661684; Thu, 13 Oct 2022 00:17:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 83D85C433D7; Thu, 13 Oct 2022 00:17:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665620237; bh=oJ6qJGlAT422qVkxzq0How+tDhfPxj4z7/qt5SBB8uE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jLVfH4qQQ5pNDq+B/3nK8qJOcvs6DtyNzQNeZDS7qk1A0Ec60Cwm5IFXZB3sV/sdJ UYKitMIicmVTZpgVxGB1/bwDmzR8eJm0LgQycIgEf12I5h3hf1f8zGEeJlgMYgnkMR B6Hde6TMzMeqEXK27HQIxK6Ohr3Yw5NOyASq9lAPikv9/rNBCdk9g3nF8hESI3A2Xy g4zT0xKpPtRIli1/wmre9TBUkxNYrBgSmCGgphzckTZ4fGu4S7me+bb/Oz/hxEn09Z f0rK785lLW8JgXWPoUYudKB9F+5Oo7MF8RT5AEEFm6mxpyjYBY4C3Opfz5Q7IiJUIP apYw9udcSfK6g== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Johnothan King , Arne Wendt , Benjamin Tissoires , Sasha Levin , djogorchock@gmail.com, jikos@kernel.org, linux-input@vger.kernel.org Subject: [PATCH AUTOSEL 6.0 34/67] HID: nintendo: check analog user calibration for plausibility Date: Wed, 12 Oct 2022 20:15:15 -0400 Message-Id: <20221013001554.1892206-34-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221013001554.1892206-1-sashal@kernel.org> References: <20221013001554.1892206-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,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 From: Johnothan King [ Upstream commit 50503e360eeb968a3d00234c9cc4057d774c3e9a ] 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 = center = max = 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 Signed-off-by: Benjamin Tissoires Link: https://lore.kernel.org/r/gvpL2G6VwXGJPvxX5KRiu9pVjvTivgayug_jdKDY6zfuAaAqncP9BkKLosjwUXNlgVVTMfJSKfwPF1K79cKAkwGComyC21vCV3q9B3EXNkE=@protonmail.com Signed-off-by: Sasha Levin --- 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..c3774a468b22 100644 --- a/drivers/hid/hid-nintendo.c +++ b/drivers/hid/hid-nintendo.c @@ -760,12 +760,31 @@ static int joycon_read_stick_calibration(struct joycon_ctlr *ctlr, u16 cal_addr, cal_y->max = cal_y->center + y_max_above; cal_y->min = cal_y->center - y_min_below; - return 0; + /* check if calibration values are plausible */ + if (cal_x->min >= cal_x->center || cal_x->center >= cal_x->max || + cal_y->min >= cal_y->center || cal_y->center >= cal_y->max) + ret = -EINVAL; + + return ret; } static const u16 DFLT_STICK_CAL_CEN = 2000; static const u16 DFLT_STICK_CAL_MAX = 3500; static const u16 DFLT_STICK_CAL_MIN = 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=%d\n", + stick, ret); + + cal_x->center = cal_y->center = DFLT_STICK_CAL_CEN; + cal_x->max = cal_y->max = DFLT_STICK_CAL_MAX; + cal_x->min = cal_y->min = DFLT_STICK_CAL_MIN; +} + static int joycon_request_calibration(struct joycon_ctlr *ctlr) { u16 left_stick_addr = JC_CAL_FCT_DATA_LEFT_ADDR; @@ -793,38 +812,24 @@ static int joycon_request_calibration(struct joycon_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=%d\n", - ret); - - ctlr->left_stick_cal_x.center = DFLT_STICK_CAL_CEN; - ctlr->left_stick_cal_x.max = DFLT_STICK_CAL_MAX; - ctlr->left_stick_cal_x.min = DFLT_STICK_CAL_MIN; - ctlr->left_stick_cal_y.center = DFLT_STICK_CAL_CEN; - ctlr->left_stick_cal_y.max = DFLT_STICK_CAL_MAX; - ctlr->left_stick_cal_y.min = DFLT_STICK_CAL_MIN; - } + if (ret) + joycon_use_default_calibration(ctlr->hdev, + &ctlr->left_stick_cal_x, + &ctlr->left_stick_cal_y, + "left", ret); /* read the right stick calibration data */ ret = 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=%d\n", - ret); - - ctlr->right_stick_cal_x.center = DFLT_STICK_CAL_CEN; - ctlr->right_stick_cal_x.max = DFLT_STICK_CAL_MAX; - ctlr->right_stick_cal_x.min = DFLT_STICK_CAL_MIN; - ctlr->right_stick_cal_y.center = DFLT_STICK_CAL_CEN; - ctlr->right_stick_cal_y.max = DFLT_STICK_CAL_MAX; - ctlr->right_stick_cal_y.min = DFLT_STICK_CAL_MIN; - } + if (ret) + joycon_use_default_calibration(ctlr->hdev, + &ctlr->right_stick_cal_x, + &ctlr->right_stick_cal_y, + "right", ret); hid_dbg(ctlr->hdev, "calibration:\n" "l_x_c=%d l_x_max=%d l_x_min=%d\n" -- 2.35.1