From patchwork Fri Jan 6 01:59:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roderick Colenbrander X-Patchwork-Id: 13090783 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 14B30C4708E for ; Fri, 6 Jan 2023 01:59:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230131AbjAFB7Z (ORCPT ); Thu, 5 Jan 2023 20:59:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230015AbjAFB7X (ORCPT ); Thu, 5 Jan 2023 20:59:23 -0500 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4383659F84 for ; Thu, 5 Jan 2023 17:59:22 -0800 (PST) Received: by mail-pj1-x1035.google.com with SMTP id n65-20020a17090a2cc700b0021bc5ef7a14so352573pjd.0 for ; Thu, 05 Jan 2023 17:59:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gaikai-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=B9e3tQmCS4HDkYyqjGtAMjGuh58uLx96dhO77mVRrWg=; b=mqvjKTmXIzZkOF/lpwJfV9NdDnk1kx8salVG4ZyhKM6xC3HZa9DAFkAndySYxx5bGW 1rscjfFtg6GsZtLVUxFa3o9zjCvBKXKfm8kcb6L+m/VtfoxFmKJ+rghZkvLhWeTx91b7 fsataEHmaCA9h+ZeLBeSHjUbS4ava2+Wq49URWwHI6RzYgqMqVyfji2+/+ar1MPCj7dg C/gkY5/HypYzdcpRhaYLee0EZ7th3FJo86dpJOjdnnx0sZ3hbopAvQeYcw0lVkpD7GmB pfcFyXi3an2L/FZdfkLwcw2DsJe3Zk2SZVLjXLa5xEpcSBRMVCAbgS8yQtoeYiA3mC8J Uh4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=B9e3tQmCS4HDkYyqjGtAMjGuh58uLx96dhO77mVRrWg=; b=qxnLzixqRapRB06Py+IcC96rgnTct6pFWxswNOHb7aRnutuI6feX4+ioQNjPvcNL9F xudsmZALKSxiP5lmZIb+NNb0AqooxKQIDQIAc2szVG8WXhwfnODwWus56qPxDpulPpND CA/yz08Aw0D0YsZk9WNUSlc0h+JBk/9rQLfcJ9VL2b5qW0e1Vv1oBouWswiK78rNq0TI r4bw9t0/3Jnbb4HSK14qf9teBdrBpDrcceGlbvHDbwnCXabWSIkoIp+/mwsxPCI0k8Oq RpZWzUymzbOcvlkOS1pfot2YMROMWYog92BuwTXQjEi+4RYeTdQn2323KvvFwpcMMtpZ nsNA== X-Gm-Message-State: AFqh2ko+FObxbq4gZk6YcTRb01D10AR2OhdMgrvh7Jn3kPhkZrYwgDQi yh2C9lKd/gbIe/OBzGRzkwZeg43i8GrNXgww X-Google-Smtp-Source: AMrXdXupXAyNXsGzqVC6a+ZTPgOoldzK1a1QRfdMOU18JcoSxZCI/AUpggEYjbNMFfky7+vJ4gawdQ== X-Received: by 2002:a17:902:6b02:b0:191:1c99:57ef with SMTP id o2-20020a1709026b0200b001911c9957efmr60373840plk.67.1672970361853; Thu, 05 Jan 2023 17:59:21 -0800 (PST) Received: from localhost.localdomain (23-122-157-100.lightspeed.irvnca.sbcglobal.net. [23.122.157.100]) by smtp.gmail.com with ESMTPSA id i18-20020a17090332d200b001897a8b537asm26811165plr.221.2023.01.05.17.59.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Jan 2023 17:59:21 -0800 (PST) From: Roderick Colenbrander X-Google-Original-From: Roderick Colenbrander To: Jiri Kosina , Benjamin Tissoires Cc: linux-input@vger.kernel.org, Roderick Colenbrander , Abaci Robot , Jiapeng Chong , kernel test robot , Dan Carpenter Subject: [PATCH 1/5] HID: playstation: fix DualShock4 unreachable calibration code. Date: Thu, 5 Jan 2023 17:59:06 -0800 Message-Id: <20230106015910.3031670-2-roderick.colenbrander@sony.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230106015910.3031670-1-roderick.colenbrander@sony.com> References: <20230106015910.3031670-1-roderick.colenbrander@sony.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org Reported-by: Abaci Robot Reported-by: Jiapeng Chong Reported-by: kernel test robot Reported-by: Dan Carpenter Signed-off-by: Roderick Colenbrander --- drivers/hid/hid-playstation.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c index f399bf0d3c8c..866cc4e94320 100644 --- a/drivers/hid/hid-playstation.c +++ b/drivers/hid/hid-playstation.c @@ -1759,11 +1759,10 @@ static int dualshock4_get_calibration_data(struct dualshock4 *ds4) if (retries < 2) { hid_warn(hdev, "Retrying DualShock 4 get calibration report (0x02) request\n"); continue; - } else { - ret = -EILSEQ; - goto err_free; } + hid_err(hdev, "Failed to retrieve DualShock4 calibration info: %d\n", ret); + ret = -EILSEQ; goto err_free; } else { break; From patchwork Fri Jan 6 01:59:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roderick Colenbrander X-Patchwork-Id: 13090784 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF780C53210 for ; Fri, 6 Jan 2023 01:59:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230015AbjAFB70 (ORCPT ); Thu, 5 Jan 2023 20:59:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229455AbjAFB7Z (ORCPT ); Thu, 5 Jan 2023 20:59:25 -0500 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4994D59FA1 for ; Thu, 5 Jan 2023 17:59:23 -0800 (PST) Received: by mail-pj1-x102a.google.com with SMTP id v23so137881pju.3 for ; Thu, 05 Jan 2023 17:59:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gaikai-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9q/yuKVRjoakMYSlGhdaHUFr1ncsBSOmYAvw2qMX5d8=; b=jVz4Ix8plCSR6xxYoNaV0vP33Nvk6RZLE2wKClOOEEZdrpTPc4CJhIRdq5VRkck2EI cwIe44M1VYzgIZuUI/VMxtf26bxZZBnRld6AaOZC61Iq/sylVMi6fsL1unE7oI25Thbp w8dvGHN8T+oNT4vgAY+vFCyopxlMF29bxQn4LNEghy7rv2Sx6ZM33s/MqZqZigNb6LSz bKV4jMAQLkol92fac7qlzmA5v28YQChrSznHqm2owiEl6rk/oWjDv1jPR/WVyFtQZTRW 9teQN1Vpixgi822o85yiSRIMba5b+ByN8Efd3eAK350PTtqmEY2o38wLFppJVLfdRRwY flIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9q/yuKVRjoakMYSlGhdaHUFr1ncsBSOmYAvw2qMX5d8=; b=LN+xoLWgtWQJAu3C+UnD0G4CQRTQCX/0wt9N0THyRP/0VFmdcA7fuYy8OR3X0hjfzy OWKjyKNm7dBKR8rBEzhfzs3lnuQB9Akvs0Hji2fn4HkbsulHJyymR1fH7Q8xnfcGV+Pg ajPFyBgBIzYNCrqiybbaVYNmfBuQsPjaypgF4oHei+XCfnfEMmA8RPQGESth3/862qFl DA5FSyP/u/3enynHm5MhzUMNxmmVmyygXkKw9lTe9AYFYZZADJ4AZ0ztFE46tZIV4Ym9 b18oMFBI9P609RAl2WxOk0NLwYWiH5sbT1GkPRNHZddrLxOhTUx+C6Vxe+KjXm31HsA2 Rxdw== X-Gm-Message-State: AFqh2kqIq99ChqXGmjn00TDPC5FhTyhX4epr8zkGAU1s9nLLUOi0rJDj bO9CtHHa8SrizQ8Rj3ufm/EK9g== X-Google-Smtp-Source: AMrXdXu5SYX6jetdXIDZJ9TJbvFKu1+EijF43vnSBKsXcjfdnevRtGutah0NEySHa2lxQYzSCYdh0Q== X-Received: by 2002:a17:902:a5cc:b0:192:f999:1e73 with SMTP id t12-20020a170902a5cc00b00192f9991e73mr5823869plq.51.1672970362804; Thu, 05 Jan 2023 17:59:22 -0800 (PST) Received: from localhost.localdomain (23-122-157-100.lightspeed.irvnca.sbcglobal.net. [23.122.157.100]) by smtp.gmail.com with ESMTPSA id i18-20020a17090332d200b001897a8b537asm26811165plr.221.2023.01.05.17.59.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Jan 2023 17:59:22 -0800 (PST) From: Roderick Colenbrander X-Google-Original-From: Roderick Colenbrander To: Jiri Kosina , Benjamin Tissoires Cc: linux-input@vger.kernel.org, Roderick Colenbrander , Alain Carlucci Subject: [PATCH 2/5] HID: playstation: sanity check DualShock4 calibration data. Date: Thu, 5 Jan 2023 17:59:07 -0800 Message-Id: <20230106015910.3031670-3-roderick.colenbrander@sony.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230106015910.3031670-1-roderick.colenbrander@sony.com> References: <20230106015910.3031670-1-roderick.colenbrander@sony.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org Some DualShock4 devices report invalid calibration data resulting in kernel oopses due to division by zero during report handling. The devices affected generally appear to be clone devices, which don't implement all reports properly and don't populate proper calibration data. The issue may have been seen on an official device with erased calibration reports. This patch prevents the crashes by essentially disabling calibration when invalid values are detected. Signed-off-by: Roderick Colenbrander Tested-by: Alain Carlucci --- drivers/hid/hid-playstation.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c index 866cc4e94320..783ce9fd396e 100644 --- a/drivers/hid/hid-playstation.c +++ b/drivers/hid/hid-playstation.c @@ -1737,6 +1737,7 @@ static int dualshock4_get_calibration_data(struct dualshock4 *ds4) int speed_2x; int range_2g; int ret = 0; + int i; uint8_t *buf; if (ds4->base.hdev->bus == BUS_USB) { @@ -1829,6 +1830,21 @@ static int dualshock4_get_calibration_data(struct dualshock4 *ds4) ds4->gyro_calib_data[2].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S; ds4->gyro_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus; + /* + * Sanity check gyro calibration data. This is needed to prevent crashes + * during report handling of virtual, clone or broken devices not implementing + * calibration data properly. + */ + for (i = 0; i < ARRAY_SIZE(ds4->gyro_calib_data); i++) { + if (ds4->gyro_calib_data[i].sens_denom == 0) { + hid_warn(hdev, "Invalid gyro calibration data for axis (%d), disabling calibration.", + ds4->gyro_calib_data[i].abs_code); + ds4->gyro_calib_data[i].bias = 0; + ds4->gyro_calib_data[i].sens_numer = DS4_GYRO_RANGE; + ds4->gyro_calib_data[i].sens_denom = S16_MAX; + } + } + /* * Set accelerometer calibration and normalization parameters. * Data values will be normalized to 1/DS4_ACC_RES_PER_G g. @@ -1851,6 +1867,21 @@ static int dualshock4_get_calibration_data(struct dualshock4 *ds4) ds4->accel_calib_data[2].sens_numer = 2*DS4_ACC_RES_PER_G; ds4->accel_calib_data[2].sens_denom = range_2g; + /* + * Sanity check accelerometer calibration data. This is needed to prevent crashes + * during report handling of virtual, clone or broken devices not implementing calibration + * data properly. + */ + for (i = 0; i < ARRAY_SIZE(ds4->accel_calib_data); i++) { + if (ds4->accel_calib_data[i].sens_denom == 0) { + hid_warn(hdev, "Invalid accelerometer calibration data for axis (%d), disabling calibration.", + ds4->accel_calib_data[i].abs_code); + ds4->accel_calib_data[i].bias = 0; + ds4->accel_calib_data[i].sens_numer = DS4_ACC_RANGE; + ds4->accel_calib_data[i].sens_denom = S16_MAX; + } + } + err_free: kfree(buf); return ret; From patchwork Fri Jan 6 01:59:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roderick Colenbrander X-Patchwork-Id: 13090785 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 775B7C54E76 for ; Fri, 6 Jan 2023 01:59:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230194AbjAFB71 (ORCPT ); Thu, 5 Jan 2023 20:59:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229516AbjAFB7Z (ORCPT ); Thu, 5 Jan 2023 20:59:25 -0500 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28DBB59FA2 for ; Thu, 5 Jan 2023 17:59:24 -0800 (PST) Received: by mail-pj1-x1035.google.com with SMTP id n65-20020a17090a2cc700b0021bc5ef7a14so352622pjd.0 for ; Thu, 05 Jan 2023 17:59:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gaikai-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=N+CpugAzMZ+0pmHShY9GZuJJGn7DazaPnxuhhJHzogs=; b=PxRMH8b2owZn0RVjX0BPJ++27K8nWn/4MLpO3cyu77APLNmCd8RK8DJxSU+b21VIW2 WcQwDKma1vZwLDmTAvFxYV4zh7mfh5nw4ymgAJpTwil7bNa43jzlPEI5FZSQ7fcn6yeU w3icHPVP6ExIQhmLUCw05BT0n7YccXBXTC2RiTOCvPhf7oRXNajs46gNlV6FPN7uhyfy TqcrgMZzROWyLdc3sf6wN/t7gJsUDiCfG7nLzwgTm9Kx7N2FSxg+ICu342bgpH+SdR6X 7SfEUxka//lPC32t6FQy7rzTKNYbMs1Ycq5nOocxlhFAQ7QHMp8c0e9sq18IYTZDxFtS kuRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=N+CpugAzMZ+0pmHShY9GZuJJGn7DazaPnxuhhJHzogs=; b=vNrpR6UmqqT4eQJVR1S87TgIuR6Xux+sIfHTzvP15WvTg6HlqFdxYCaAw+o/rkeOzN YuXmP4qJawUQP/j6/9E5yuMKrZt2gxleI9zbbSu9hTi0ZJLFG6m5SnsJisy6SmKYaV58 Zuk8oXBpRbHL2jpeXQJx34foZpr0BrS1IXhMYBfoDauFwQZrkfN0ZMpNIt2UVnb6Rki8 woWj44sN+Fp8xwoH/BqNgViHisI4nI/nUe0+4RAj+hghO2X4MWAwOMoqd29br5ocQ1KI Du49NB41YGJPonWuM3EwYCwEw7jdlph/8ng6043CDoWKwWvIlARRUBnDfDimGYRj0xD2 jd7g== X-Gm-Message-State: AFqh2krhBsB78AbQviky6gPDhqjFPX1W3vNyhAa7MByngbqQ9QYFLVtx bOrpSVtQ7IYvAe3XfLIMPRbgLg== X-Google-Smtp-Source: AMrXdXtpo/p5cOfciy9g1fLSX0S9OczDZjRnhFq8AxjRs1CxvGQzrjky+rslT2K1YKogMOnBWnHXsg== X-Received: by 2002:a17:902:8601:b0:189:c4a9:c5e8 with SMTP id f1-20020a170902860100b00189c4a9c5e8mr51107984plo.45.1672970363849; Thu, 05 Jan 2023 17:59:23 -0800 (PST) Received: from localhost.localdomain (23-122-157-100.lightspeed.irvnca.sbcglobal.net. [23.122.157.100]) by smtp.gmail.com with ESMTPSA id i18-20020a17090332d200b001897a8b537asm26811165plr.221.2023.01.05.17.59.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Jan 2023 17:59:23 -0800 (PST) From: Roderick Colenbrander X-Google-Original-From: Roderick Colenbrander To: Jiri Kosina , Benjamin Tissoires Cc: linux-input@vger.kernel.org, Roderick Colenbrander Subject: [PATCH 3/5] HID: playstation: sanity check DualSense calibration data. Date: Thu, 5 Jan 2023 17:59:08 -0800 Message-Id: <20230106015910.3031670-4-roderick.colenbrander@sony.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230106015910.3031670-1-roderick.colenbrander@sony.com> References: <20230106015910.3031670-1-roderick.colenbrander@sony.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org Make sure calibration values are defined to prevent potential kernel crashes. This fixes a hypothetical issue for virtual or clone devices inspired by a similar fix for DS4. Signed-off-by: Roderick Colenbrander --- drivers/hid/hid-playstation.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c index 783ce9fd396e..11bb6caf7412 100644 --- a/drivers/hid/hid-playstation.c +++ b/drivers/hid/hid-playstation.c @@ -944,6 +944,7 @@ ATTRIBUTE_GROUPS(ps_device); static int dualsense_get_calibration_data(struct dualsense *ds) { + struct hid_device *hdev = ds->base.hdev; short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus; short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus; short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus; @@ -954,6 +955,7 @@ static int dualsense_get_calibration_data(struct dualsense *ds) int speed_2x; int range_2g; int ret = 0; + int i; uint8_t *buf; buf = kzalloc(DS_FEATURE_REPORT_CALIBRATION_SIZE, GFP_KERNEL); @@ -1005,6 +1007,21 @@ static int dualsense_get_calibration_data(struct dualsense *ds) ds->gyro_calib_data[2].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S; ds->gyro_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus; + /* + * Sanity check gyro calibration data. This is needed to prevent crashes + * during report handling of virtual, clone or broken devices not implementing + * calibration data properly. + */ + for (i = 0; i < ARRAY_SIZE(ds->gyro_calib_data); i++) { + if (ds->gyro_calib_data[i].sens_denom == 0) { + hid_warn(hdev, "Invalid gyro calibration data for axis (%d), disabling calibration.", + ds->gyro_calib_data[i].abs_code); + ds->gyro_calib_data[i].bias = 0; + ds->gyro_calib_data[i].sens_numer = DS_GYRO_RANGE; + ds->gyro_calib_data[i].sens_denom = S16_MAX; + } + } + /* * Set accelerometer calibration and normalization parameters. * Data values will be normalized to 1/DS_ACC_RES_PER_G g. @@ -1027,6 +1044,21 @@ static int dualsense_get_calibration_data(struct dualsense *ds) ds->accel_calib_data[2].sens_numer = 2*DS_ACC_RES_PER_G; ds->accel_calib_data[2].sens_denom = range_2g; + /* + * Sanity check accelerometer calibration data. This is needed to prevent crashes + * during report handling of virtual, clone or broken devices not implementing calibration + * data properly. + */ + for (i = 0; i < ARRAY_SIZE(ds->accel_calib_data); i++) { + if (ds->accel_calib_data[i].sens_denom == 0) { + hid_warn(hdev, "Invalid accelerometer calibration data for axis (%d), disabling calibration.", + ds->accel_calib_data[i].abs_code); + ds->accel_calib_data[i].bias = 0; + ds->accel_calib_data[i].sens_numer = DS_ACC_RANGE; + ds->accel_calib_data[i].sens_denom = S16_MAX; + } + } + err_free: kfree(buf); return ret; From patchwork Fri Jan 6 01:59:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roderick Colenbrander X-Patchwork-Id: 13090786 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BC8FDC53210 for ; Fri, 6 Jan 2023 01:59:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229516AbjAFB71 (ORCPT ); Thu, 5 Jan 2023 20:59:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229652AbjAFB70 (ORCPT ); Thu, 5 Jan 2023 20:59:26 -0500 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BE7464EE for ; Thu, 5 Jan 2023 17:59:25 -0800 (PST) Received: by mail-pj1-x1034.google.com with SMTP id z9-20020a17090a468900b00226b6e7aeeaso328378pjf.1 for ; Thu, 05 Jan 2023 17:59:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gaikai-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ywybv/yF2WVRYQMqZrha9kuU/T0qN9GFaKQmQOfjLnU=; b=KOucshe1DNGfeJ68VV0QjJ0s7BgdOGAnWLfZ0W5zNorUJFu0k2ub6h8p7WCSnHDSDd VDQreiSJUZ6usfFvZEh0aNp7khG9BFcx5BXSTaHZ1w0KMe/BvwEQ43qd03+47+LvdjHE 1kH3409HQgj8DRPLO2bPL9TzV242pdFvoEWm/klPg318buu/w+3jel4Pb8MqV8GOZoLw 73dijBZJBmm3Trg1Lr7sbwvaJ88v+SCfXIFst2nWWSmmOZppVsYNQHdKarFuGSDHDSwM +z2G0qKqVNrQZFmZM968pp5dRnYDcxYStuFyX70Si63qPXhtG23qGV1FjebYfuIEAz+C rdbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ywybv/yF2WVRYQMqZrha9kuU/T0qN9GFaKQmQOfjLnU=; b=2+WxH1hwhBmiCg+kzx/vWVXYavwrwkzbWXVm+69BFaBqpKJREOjsXri285iAtphEZm W4BohDBCcHssZGBXLC99olMdyVneoN2Zp8iZpsFScEE+8G9s4DX4OSI2F+XEMdAVAWFV e5Kxwka9PjCGZy0BdNzEgYvUHux5EiXtoNQyL5Ia8saO4FncaC03BS595biScrffuvCq IlX4a8xBtpWx/aft4u+qJdsSlnyTodiOoIkq146EASKSgt4T4+0vwpMygG5/nZMVvGdV K7Dp/4MpN6OuIIdm8LfSdjNvFNq2MzSOxogd6BoNiG2+c+FydVqZ/qv0jiHOS6R1xO+W QNjQ== X-Gm-Message-State: AFqh2kprv+bD4dlx5BSdoQJtB/LeM7P3OSfcFZXtfUJkSEzS0VShvP91 k2k9ykQ2WwrlaKl85rzv6K+pGw== X-Google-Smtp-Source: AMrXdXt1vmlzc9jRGZQVviD856ZcVO7zMkg9wBXZf5R+tyLcGqYc6Mk1842HQCRcRvWZeo1wd+/zPQ== X-Received: by 2002:a05:6a21:e384:b0:af:9539:9fc9 with SMTP id cc4-20020a056a21e38400b000af95399fc9mr73407738pzc.47.1672970364915; Thu, 05 Jan 2023 17:59:24 -0800 (PST) Received: from localhost.localdomain (23-122-157-100.lightspeed.irvnca.sbcglobal.net. [23.122.157.100]) by smtp.gmail.com with ESMTPSA id i18-20020a17090332d200b001897a8b537asm26811165plr.221.2023.01.05.17.59.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Jan 2023 17:59:24 -0800 (PST) From: Roderick Colenbrander X-Google-Original-From: Roderick Colenbrander To: Jiri Kosina , Benjamin Tissoires Cc: linux-input@vger.kernel.org, Roderick Colenbrander Subject: [PATCH 4/5] HID: playstation: correct DualShock4 gyro bias handling. Date: Thu, 5 Jan 2023 17:59:09 -0800 Message-Id: <20230106015910.3031670-5-roderick.colenbrander@sony.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230106015910.3031670-1-roderick.colenbrander@sony.com> References: <20230106015910.3031670-1-roderick.colenbrander@sony.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org The bias for the gyroscope is not used correctly. The sensor bias needs to be used in calculation of the 'sensivity' instead of being an offset. In practice this has little input on the values as the bias values tends to be small (+/- 20). Signed-off-by: Roderick Colenbrander --- drivers/hid/hid-playstation.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c index 11bb6caf7412..1001eab6ff75 100644 --- a/drivers/hid/hid-playstation.c +++ b/drivers/hid/hid-playstation.c @@ -1848,19 +1848,22 @@ static int dualshock4_get_calibration_data(struct dualshock4 *ds4) */ speed_2x = (gyro_speed_plus + gyro_speed_minus); ds4->gyro_calib_data[0].abs_code = ABS_RX; - ds4->gyro_calib_data[0].bias = gyro_pitch_bias; + ds4->gyro_calib_data[0].bias = 0; ds4->gyro_calib_data[0].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S; - ds4->gyro_calib_data[0].sens_denom = gyro_pitch_plus - gyro_pitch_minus; + ds4->gyro_calib_data[0].sens_denom = abs(gyro_pitch_plus - gyro_pitch_bias) + + abs(gyro_pitch_minus - gyro_pitch_bias); ds4->gyro_calib_data[1].abs_code = ABS_RY; - ds4->gyro_calib_data[1].bias = gyro_yaw_bias; + ds4->gyro_calib_data[1].bias = 0; ds4->gyro_calib_data[1].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S; - ds4->gyro_calib_data[1].sens_denom = gyro_yaw_plus - gyro_yaw_minus; + ds4->gyro_calib_data[1].sens_denom = abs(gyro_yaw_plus - gyro_yaw_bias) + + abs(gyro_yaw_minus - gyro_yaw_bias); ds4->gyro_calib_data[2].abs_code = ABS_RZ; - ds4->gyro_calib_data[2].bias = gyro_roll_bias; + ds4->gyro_calib_data[2].bias = 0; ds4->gyro_calib_data[2].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S; - ds4->gyro_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus; + ds4->gyro_calib_data[2].sens_denom = abs(gyro_roll_plus - gyro_roll_bias) + + abs(gyro_roll_minus - gyro_roll_bias); /* * Sanity check gyro calibration data. This is needed to prevent crashes @@ -2241,8 +2244,7 @@ static int dualshock4_parse_report(struct ps_device *ps_dev, struct hid_report * for (i = 0; i < ARRAY_SIZE(ds4_report->gyro); i++) { int raw_data = (short)le16_to_cpu(ds4_report->gyro[i]); int calib_data = mult_frac(ds4->gyro_calib_data[i].sens_numer, - raw_data - ds4->gyro_calib_data[i].bias, - ds4->gyro_calib_data[i].sens_denom); + raw_data, ds4->gyro_calib_data[i].sens_denom); input_report_abs(ds4->sensors, ds4->gyro_calib_data[i].abs_code, calib_data); } From patchwork Fri Jan 6 01:59:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roderick Colenbrander X-Patchwork-Id: 13090787 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C0FBC4708E for ; Fri, 6 Jan 2023 01:59:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230282AbjAFB7a (ORCPT ); Thu, 5 Jan 2023 20:59:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230247AbjAFB72 (ORCPT ); Thu, 5 Jan 2023 20:59:28 -0500 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73C6513E8D for ; Thu, 5 Jan 2023 17:59:26 -0800 (PST) Received: by mail-pj1-x1029.google.com with SMTP id q64so134257pjq.4 for ; Thu, 05 Jan 2023 17:59:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gaikai-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Z3nL1wIsd2dEPQPL/vQTDGsninQKYV4qJDpL3tTkIro=; b=xetaKnv5SkRP6HW50DWypVUxTxvvhoapX87M1jQBUpIbuxjVRy6GVCULceOlw5jHBQ XPPZqpbQQ7XJsD6oX0wsCpS253Nca4wvYi2awZu5XOExAXoU+mYNVUODOQYlHQ9+4SRZ NYRsktAgx2EkQrRWi2bnOEpdSE7DwW5xM2OOXUK8jGlHXu8qsgmjJcdhRfJ8GOn3V3uB 9TLahEN3yeoIWWUvyXzmAcVpoFMUA3EsHuIgMg1BO7ON6TXZhnD7Phv5+yGjOihUyKrt QWRcsNh4yGKal90BadNeG/jdmcSWstGxtwgaStFFlEt+ZLk0RG3iAl2gjwdSy4qrlSvs W3aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Z3nL1wIsd2dEPQPL/vQTDGsninQKYV4qJDpL3tTkIro=; b=aW6/kjk9al/tpNFVKKw9mt+fc2vf0x/tj3LMNBF/nekR7vesHZlBbqRRMrLHH6vMYd hZ7FKDPttVWx1K7OMg0bfdpQmw8cjCcR9C3bgJUpwGWClRsqZ6LuLHZVEAntgsF5au6Y 7zsN79chKO8nlbFBlU80AcFstD9yqkR5wQAydx7K2tE4iNajOD/QkhcKgf4nkQ6MBpuc 4wJRTozx4TXIvJgaHP6oecv2KreElEPnfnMeZgQGqmXDbJTQFChTKbEEjVtfMBIeo6gR ueSWcbasvanvRXBxmPwa+TPC2OnEU73lOIHc5uIER4u3AFjCRs5kWJ31QoXmMomcaDrb UF2Q== X-Gm-Message-State: AFqh2kqtfz440W4mfJXUzSYuenJpUke8zOshA+jEnEs/ufOgxOfSaurv 8ekrF9Rq3KFjevGEFSGvqMexCg== X-Google-Smtp-Source: AMrXdXu5+5GFPMH2LXBOnsll3v2RULLR1dBMmx+ZdSCrys8H1CMFdmURNmGt2ZQhTnH34lIoUzFoLQ== X-Received: by 2002:a17:902:ee55:b0:192:dbc3:6b79 with SMTP id 21-20020a170902ee5500b00192dbc36b79mr12467651plo.12.1672970365986; Thu, 05 Jan 2023 17:59:25 -0800 (PST) Received: from localhost.localdomain (23-122-157-100.lightspeed.irvnca.sbcglobal.net. [23.122.157.100]) by smtp.gmail.com with ESMTPSA id i18-20020a17090332d200b001897a8b537asm26811165plr.221.2023.01.05.17.59.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Jan 2023 17:59:25 -0800 (PST) From: Roderick Colenbrander X-Google-Original-From: Roderick Colenbrander To: Jiri Kosina , Benjamin Tissoires Cc: linux-input@vger.kernel.org, Roderick Colenbrander Subject: [PATCH 5/5] HID: playstation: correct DualSense gyro bias handling. Date: Thu, 5 Jan 2023 17:59:10 -0800 Message-Id: <20230106015910.3031670-6-roderick.colenbrander@sony.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230106015910.3031670-1-roderick.colenbrander@sony.com> References: <20230106015910.3031670-1-roderick.colenbrander@sony.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org The bias for the gyroscope is not used correctly. The sensor bias needs to be used in calculation of the 'sensivity' instead of being an offset. In practice this has little input on the values as the bias values tends to be small (+/- 20). Signed-off-by: Roderick Colenbrander --- drivers/hid/hid-playstation.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c index 1001eab6ff75..8ac8f7b8e317 100644 --- a/drivers/hid/hid-playstation.c +++ b/drivers/hid/hid-playstation.c @@ -993,19 +993,22 @@ static int dualsense_get_calibration_data(struct dualsense *ds) */ speed_2x = (gyro_speed_plus + gyro_speed_minus); ds->gyro_calib_data[0].abs_code = ABS_RX; - ds->gyro_calib_data[0].bias = gyro_pitch_bias; + ds->gyro_calib_data[0].bias = 0; ds->gyro_calib_data[0].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S; - ds->gyro_calib_data[0].sens_denom = gyro_pitch_plus - gyro_pitch_minus; + ds->gyro_calib_data[0].sens_denom = abs(gyro_pitch_plus - gyro_pitch_bias) + + abs(gyro_pitch_minus - gyro_pitch_bias); ds->gyro_calib_data[1].abs_code = ABS_RY; - ds->gyro_calib_data[1].bias = gyro_yaw_bias; + ds->gyro_calib_data[1].bias = 0; ds->gyro_calib_data[1].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S; - ds->gyro_calib_data[1].sens_denom = gyro_yaw_plus - gyro_yaw_minus; + ds->gyro_calib_data[1].sens_denom = abs(gyro_yaw_plus - gyro_yaw_bias) + + abs(gyro_yaw_minus - gyro_yaw_bias); ds->gyro_calib_data[2].abs_code = ABS_RZ; - ds->gyro_calib_data[2].bias = gyro_roll_bias; + ds->gyro_calib_data[2].bias = 0; ds->gyro_calib_data[2].sens_numer = speed_2x*DS_GYRO_RES_PER_DEG_S; - ds->gyro_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus; + ds->gyro_calib_data[2].sens_denom = abs(gyro_roll_plus - gyro_roll_bias) + + abs(gyro_roll_minus - gyro_roll_bias); /* * Sanity check gyro calibration data. This is needed to prevent crashes @@ -1388,8 +1391,7 @@ static int dualsense_parse_report(struct ps_device *ps_dev, struct hid_report *r for (i = 0; i < ARRAY_SIZE(ds_report->gyro); i++) { int raw_data = (short)le16_to_cpu(ds_report->gyro[i]); int calib_data = mult_frac(ds->gyro_calib_data[i].sens_numer, - raw_data - ds->gyro_calib_data[i].bias, - ds->gyro_calib_data[i].sens_denom); + raw_data, ds->gyro_calib_data[i].sens_denom); input_report_abs(ds->sensors, ds->gyro_calib_data[i].abs_code, calib_data); }