From nobody Sat Oct 4 11:14:54 2025 Received: from mail-io1-f73.google.com (mail-io1-f73.google.com [209.85.166.73]) (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 35F19322C79 for ; Mon, 18 Aug 2025 14:29:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755527353; cv=none; b=Tn4gICi01QSdkCpHMIT7/NH85fpzd2OFvA8rYNFvleCIq1+aogTI3SUKOPY2m6pUyuD1VdUOR4mTRgzUmG9xIjfKAH25rZ3a5LM1w78hGyIP1YHeL4DWxrFsL8DifQ4Iy7eKGKoAmMq1knpuvvGC8Jl3m2lOAaM/pJukMmuEh5M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755527353; c=relaxed/simple; bh=EzJdaMtvWN8urpUikh8Q4GZb6HxTthrpcmrUKVjniQw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tTy25PVaLknVvdtA/7O6D+wPYBar3sDQ39FBTBO60xKEKyzaXcgB6nU4JOxvs0zA7/ZXQgey9CC1TPgtkpEgL2OnnZ8U2hiMhq4JPVkCuSFFpRomwtGCiD4s5Zk5I/tL+u5GTYJ7LgXGnNPNDdqV0OXcGOHxRcNwERwC97vGE28= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jdenose.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xmXgomrJ; arc=none smtp.client-ip=209.85.166.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jdenose.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xmXgomrJ" Received: by mail-io1-f73.google.com with SMTP id ca18e2360f4ac-88432e1f068so475424339f.2 for ; Mon, 18 Aug 2025 07:29:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755527349; x=1756132149; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bXZVcgVQ+g42oJnrkZEqV6Ovv0XvfsNhZapIq9gDRSY=; b=xmXgomrJOfYebnTGzYvuYYgVly8cNxwJOsaFYdqCKsimViCSqalWrZRxY2h+YqL56s RhS++C4XWlY+XKhzIq5TqcmPfXMSlhNnjuoCHcxt/6Fx8zbxX6dgbTUDb8mpZf2dHPdM d1i61Rmz1ah90EOlQ0nQ/79TNjmhN+YI4fJoBwDASiWLHbXuza9mkasgEhKBSfLdEwpL DSzVBWI3d8x9MnoX93MKvbdeuYDVV7O73OyY5c6aOPv5JL/rAZq3RmQeWRibt0vHK8gL b/2Qv5FZC520NtFB2ScOGyjCh9YQ7CWprSMfAkHEblh8fq56+67furfodkwIjmDKX1ZV CraA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755527349; x=1756132149; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bXZVcgVQ+g42oJnrkZEqV6Ovv0XvfsNhZapIq9gDRSY=; b=tQEZzoW9YRmiiykJYNaFXniYRVVYrBtwB9QV7ytJvbeB3uh5zdax4CfKmAD27sRs5s aimPEDieMcZ+IvmUgk2qUDITaogJuR0gClgFaYW23+zdCiD5wkoK/IW/eZlOz5Z2fmn7 JgTmpQnAdeISX1MvrHJHbPiPPkLLdBZTbZtYMHmfgNnrle8WM8r0MvWa7NIIjJtxk1iK YDkntlWr40HD0CbCL5DAQKwgwPaLJK7KVk2SsQPNGvbCY2ejOfClMVxSHIVgs34jg4vK f0rCd67KBJg3fDLvWWGQm+hyWFblOzkW21CaRQbY82MtodFZE7VhPhs9pIP+YxWbawqU XhPg== X-Forwarded-Encrypted: i=1; AJvYcCVMIR1oP0T/MyLJ1r9IuDBeLbcFyuvG2gv5SP+wIn4eZaL2TlSnUzCSrf/kZDC+UX1FbOW3P46nCY0TI3g=@vger.kernel.org X-Gm-Message-State: AOJu0Yy6sLGelpjBt5WIx3kaZyI3sGP5KKJXWne5lzgaDq6eugs/RqAQ sj0DcFAd/p6rVzaQUf2bCZ1tbjZ7GCEfpKYclTb+/NVvZWgUzhFmT0/rTGzQX5d/i5d1Wm3iuYJ udeu/jV5c8A== X-Google-Smtp-Source: AGHT+IERQLDHL1rvPQ/kzY3xQlBsZ9A5yJyKXeDv4afvAsJ2FJMmdxwbnVaibbx2bSVgs951jmpuwDTgQ6b9 X-Received: from jay8.prod.google.com ([2002:a05:6638:c2d8:b0:4f6:9195:b4a3]) (user=jdenose job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6e02:1a4d:b0:3e5:7282:4a94 with SMTP id e9e14a558f8ab-3e583928632mr203922345ab.23.1755527349380; Mon, 18 Aug 2025 07:29:09 -0700 (PDT) Date: Mon, 18 Aug 2025 14:28:10 +0000 In-Reply-To: <20250818-support-forcepads-v2-0-ca2546e319d5@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250818-support-forcepads-v2-0-ca2546e319d5@google.com> X-Mailer: b4 0.14.2 Message-ID: <20250818-support-forcepads-v2-10-ca2546e319d5@google.com> Subject: [PATCH v2 10/11] HID: haptic: add hid_haptic_switch_mode From: Jonathan Denose To: Jiri Kosina , Benjamin Tissoires , Dmitry Torokhov , Jonathan Corbet , Henrik Rydberg Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Angela Czubak , "Sean O'Brien" , Jonathan Denose Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Angela Czubak Function hid_haptic_switch_mode() can be used to switch between device-controlled mode and host-controlled mode. Uploading a WAVEFORMPRESS or WAVEFORMRELEASE effect triggers host-controlled mode if the device is in device-controlled mode. Signed-off-by: Angela Czubak Co-developed-by: Jonathan Denose Signed-off-by: Jonathan Denose --- drivers/hid/hid-haptic.c | 66 +++++++++++++++++++++++++++++++++++++++++++-= ---- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c index c02af820051c22d1c899db84496c5a44b868fe49..aa090684c1f23b61a1ac4e9e7e5= 23b31a8166a21 100644 --- a/drivers/hid/hid-haptic.c +++ b/drivers/hid/hid-haptic.c @@ -5,6 +5,7 @@ * Copyright (c) 2021 Angela Czubak */ =20 +#include #include =20 #include "hid-haptic.h" @@ -197,12 +198,46 @@ static void fill_effect_buf(struct hid_haptic_device = *haptic, mutex_unlock(&haptic->manual_trigger_mutex); } =20 +static void switch_mode(struct hid_device *hdev, struct hid_haptic_device = *haptic, + int mode) +{ + struct hid_report *rep =3D haptic->auto_trigger_report; + struct hid_field *field; + s32 value; + int i, j; + + if (mode =3D=3D HID_HAPTIC_MODE_HOST) + value =3D HID_HAPTIC_ORDINAL_WAVEFORMSTOP; + else + value =3D haptic->default_auto_trigger; + + mutex_lock(&haptic->auto_trigger_mutex); + for (i =3D 0; i < rep->maxfield; i++) { + field =3D rep->field[i]; + /* Ignore if report count is out of bounds. */ + if (field->report_count < 1) + continue; + + for (j =3D 0; j < field->maxusage; j++) { + if (field->usage[j].hid =3D=3D HID_HP_AUTOTRIGGER) + field->value[j] =3D value; + } + } + + /* send the report */ + hid_hw_request(hdev, rep, HID_REQ_SET_REPORT); + mutex_unlock(&haptic->auto_trigger_mutex); + haptic->mode =3D mode; +} + static int hid_haptic_upload_effect(struct input_dev *dev, struct ff_effec= t *effect, struct ff_effect *old) { + struct hid_device *hdev =3D input_get_drvdata(dev); struct ff_device *ff =3D dev->ff; struct hid_haptic_device *haptic =3D ff->private; int i, ordinal =3D 0; + bool switch_modes =3D false; =20 /* If vendor range, check vendor id and page */ if (effect->u.haptic.hid_usage >=3D (HID_HP_VENDORWAVEFORMMIN & HID_USAGE= ) && @@ -225,6 +260,16 @@ static int hid_haptic_upload_effect(struct input_dev *= dev, struct ff_effect *eff fill_effect_buf(haptic, &effect->u.haptic, &haptic->effect[effect->id], ordinal); =20 + if (effect->u.haptic.hid_usage =3D=3D (HID_HP_WAVEFORMPRESS & HID_USAGE) = || + effect->u.haptic.hid_usage =3D=3D (HID_HP_WAVEFORMRELEASE & HID_USAGE)) + switch_modes =3D true; + + /* If device is in autonomous mode, and the uploaded effect signals users= pace + * wants control of the device, change modes + */ + if (switch_modes && haptic->mode =3D=3D HID_HAPTIC_MODE_DEVICE) + switch_mode(hdev, haptic, HID_HAPTIC_MODE_HOST); + return 0; } =20 @@ -290,6 +335,7 @@ static void effect_set_default(struct ff_effect *effect) static int hid_haptic_erase(struct input_dev *dev, int effect_id) { struct hid_haptic_device *haptic =3D dev->ff->private; + struct hid_device *hdev =3D input_get_drvdata(dev); struct ff_effect effect; int ordinal; =20 @@ -297,20 +343,25 @@ static int hid_haptic_erase(struct input_dev *dev, in= t effect_id) =20 if (effect.u.haptic.hid_usage =3D=3D (HID_HP_WAVEFORMRELEASE & HID_USAGE)= ) { ordinal =3D haptic->release_ordinal; - if (!ordinal) + if (!ordinal) { ordinal =3D HID_HAPTIC_ORDINAL_WAVEFORMNONE; - else - effect.u.haptic.hid_usage =3D HID_HP_WAVEFORMRELEASE & - HID_USAGE; + if (haptic->mode =3D=3D HID_HAPTIC_MODE_HOST) + switch_mode(hdev, haptic, HID_HAPTIC_MODE_DEVICE); + } else + effect.u.haptic.hid_usage =3D HID_HP_WAVEFORMRELEASE & HID_USAGE; + fill_effect_buf(haptic, &effect.u.haptic, &haptic->effect[effect_id], ordinal); } else if (effect.u.haptic.hid_usage =3D=3D (HID_HP_WAVEFORMPRESS & HID_U= SAGE)) { ordinal =3D haptic->press_ordinal; - if (!ordinal) + if (!ordinal) { ordinal =3D HID_HAPTIC_ORDINAL_WAVEFORMNONE; + if (haptic->mode =3D=3D HID_HAPTIC_MODE_HOST) + switch_mode(hdev, haptic, HID_HAPTIC_MODE_DEVICE); + } else - effect.u.haptic.hid_usage =3D HID_HP_WAVEFORMPRESS & - HID_USAGE; + effect.u.haptic.hid_usage =3D HID_HP_WAVEFORMPRESS & HID_USAGE; + fill_effect_buf(haptic, &effect.u.haptic, &haptic->effect[effect_id], ordinal); } @@ -392,6 +443,7 @@ int hid_haptic_init(struct hid_device *hdev, haptic->hid_usage_map[HID_HAPTIC_ORDINAL_WAVEFORMSTOP] =3D HID_HP_WAVEFORMSTOP & HID_USAGE; =20 + mutex_init(&haptic->auto_trigger_mutex); for (r =3D 0; r < haptic->auto_trigger_report->maxfield; r++) parse_auto_trigger_field(haptic, haptic->auto_trigger_report->field[r]); =20 --=20 2.51.0.rc1.163.g2494970778-goog