From patchwork Sat Feb 1 11:38:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Tomasz_Paku=C5=82a?= X-Patchwork-Id: 861374 Received: from mail-ed1-f45.google.com (mail-ed1-f45.google.com [209.85.208.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB1A016F858; Sat, 1 Feb 2025 11:39:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738409955; cv=none; b=f0uFdDL02UtlBuD18wNenOFzWpy8WYtrtBU2rdXAahHdPjJ3ECC13cwXApXKq1JddcHoP4JNghvqom5pBLP72T+pKZw18oTmllYeM7B2u/jn33oTkjIg2wRHwMz59DfPQsQmDAgqgyLb4CB2t9FW6QdE0sbH41b4PgYz1Huls8U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738409955; c=relaxed/simple; bh=vT0NxbdrI6fSbYoeYU2IxQVUUt17vurL1Wne66n5fmg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=XVFC4cftTNrbPt2MbWZCgMhShx8GqubRbO6vzgpTeVQHhcP5H8e0jonsFVktz2uB7YpkDXXHiJijzyybVb+hXXfpu6fsLxaqhYvbjnuAUUbj0bp6Jl2PsD1xok3hRtnOkzRq1BUPD1MXnZOjwErTvFvvuFxlxpruf/mCx62eDbs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=NQkZ9Bz0; arc=none smtp.client-ip=209.85.208.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NQkZ9Bz0" Received: by mail-ed1-f45.google.com with SMTP id 4fb4d7f45d1cf-5d3ea2a5a9fso578444a12.2; Sat, 01 Feb 2025 03:39:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738409952; x=1739014752; darn=vger.kernel.org; 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=I38Qk1RTm5p9rwY7xvBkTCqFaYfLmI8D5AidGoGrF4o=; b=NQkZ9Bz0YA2Uttfa1C2UjYEFC22hKnJFMLLrpGoRmyHcaTpInPKGPfOHhbpKiGiDS6 NPp48XcjPKFgmAvu/o9tVNjlA96Cn4KnV7ZdR5wvEJ1jETC5gTJAul9FRcSMNwIC4Sv7 DrZYZofXMmpBxMbaECprN2MgiEzLKnO3nWKbvr73K0HXKSJu6xtuo8C/43UtIEkGg57T zDYsgeBOS4nEDOASPunQrsbcqE8JuOtFZI2IgU46rZOkoE/rr4xSRcoTbaIvHuwaMxZn QVrn27e0hles6N+4iFb5HuRboUbOQqyowHJVYNc6MGRYXKlPMYmbyE31zwx23IUbUOhz 2v+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738409952; x=1739014752; 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=I38Qk1RTm5p9rwY7xvBkTCqFaYfLmI8D5AidGoGrF4o=; b=OpF+VKdTprqu7cNWmyf0gYNAVAToANDL9ELCLyDhzR5fOox435/yaAARPP1jiFci3q Gr5k+XFi2Rq/eDS+GS4+qgmED2h67i+z+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d2:7262:9492 with SMTP id 4fb4d7f45d1cf-5dc6e6e53eamr4360160a12.2.1738409951890; Sat, 01 Feb 2025 03:39:11 -0800 (PST) Received: from laptok.lan (89-64-31-140.dynamic.chello.pl. [89.64.31.140]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5dc723e9fa7sm4304577a12.20.2025.02.01.03.39.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 01 Feb 2025 03:39:11 -0800 (PST) From: =?utf-8?q?Tomasz_Paku=C5=82a?= To: jikos@kernel.org, bentiss@kernel.org Cc: anssi.hannula@gmail.com, oleg@makarenk.ooo, linux-input@vger.kernel.org, linux-usb@vger.kernel.org Subject: [PATCH v9 03/22] HID: pidff: Clamp PERIODIC effect period to device's logical range Date: Sat, 1 Feb 2025 12:38:47 +0100 Message-ID: <20250201113906.769162-4-tomasz.pakula.oficjalny@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250201113906.769162-1-tomasz.pakula.oficjalny@gmail.com> References: <20250201113906.769162-1-tomasz.pakula.oficjalny@gmail.com> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This ensures the effect can actually be played on the connected force feedback device. Adds clamping functions used instead of rescaling, as we don't want to change the characteristics of the periodic effects. Fixes edge cases found on Moza Racing and some other hardware where the effects would not play if the period is outside the defined logical range. Changes in v6: - Use in-kernel clamp macro instead of a custom solution Signed-off-by: Tomasz Pakuła Reviewed-by: Michał Kopeć Reviewed-by: Paul Dino Jones Tested-by: Paul Dino Jones Tested-by: Cristóferson Bueno Tested-by: Pablo Cisneros --- drivers/hid/usbhid/hid-pidff.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/drivers/hid/usbhid/hid-pidff.c b/drivers/hid/usbhid/hid-pidff.c index a01c1b2ab2f4..488e6a6a14a6 100644 --- a/drivers/hid/usbhid/hid-pidff.c +++ b/drivers/hid/usbhid/hid-pidff.c @@ -15,10 +15,9 @@ #include #include #include - #include +#include -#include "usbhid.h" #define PID_EFFECTS_MAX 64 #define PID_INFINITE 0xffff @@ -187,6 +186,16 @@ struct pidff_device { int pid_id[PID_EFFECTS_MAX]; }; +/* + * Clamp value for a given field + */ +static s32 pidff_clamp(s32 i, struct hid_field *field) +{ + s32 clamped = clamp(i, field->logical_minimum, field->logical_maximum); + pr_debug("clamped from %d to %d", i, clamped); + return clamped; +} + /* * Scale an unsigned value with range 0..max for the given field */ @@ -361,7 +370,11 @@ static void pidff_set_periodic_report(struct pidff_device *pidff, pidff_set_signed(&pidff->set_periodic[PID_OFFSET], effect->u.periodic.offset); pidff_set(&pidff->set_periodic[PID_PHASE], effect->u.periodic.phase); - pidff->set_periodic[PID_PERIOD].value[0] = effect->u.periodic.period; + + /* Clamp period to ensure the device can play the effect */ + pidff->set_periodic[PID_PERIOD].value[0] = + pidff_clamp(effect->u.periodic.period, + pidff->set_periodic[PID_PERIOD].field); hid_hw_request(pidff->hid, pidff->reports[PID_SET_PERIODIC], HID_REQ_SET_REPORT);