From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-io1-f74.google.com (mail-io1-f74.google.com [209.85.166.74]) (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 6E1C3261585 for ; Mon, 14 Jul 2025 15:09:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752505791; cv=none; b=ak5t3bsPzesnhPSwtjoazyk/NTCXkmGPKZ/PxgL5skEeKpLx0MVxm5M+zaVzDeIvgwAmTEv/55/yyry4mXazXB5Ax0keTcdjE6BcFOCBpAk+2XSyY076vLOgRzaOzxE+O56pXsyt7Q1uL8u7O5PFGpGEy0V/q2D2s95JhOD8FVY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752505791; c=relaxed/simple; bh=5k4hOmm5FsRqTINdSqHY+MKywj3slz9dJzmfWE/9h3o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oDUUSM9diz2Sb0mrlLfiL963uBXua4o4vDlT+9y2DAQCkyW7aoql+4BG18xcSUse/KWK2ZY88UOtQySiteM4FWeYIio9D0WffXWSsFjbEIOTiXxt6TnFSCBNA1I3h0trNzZ/3MlF9l87lMRtGbP5QcWJVfJMJcTUsMQco05XwGw= 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=V/hjgysu; arc=none smtp.client-ip=209.85.166.74 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="V/hjgysu" Received: by mail-io1-f74.google.com with SMTP id ca18e2360f4ac-86cfccca327so919529139f.2 for ; Mon, 14 Jul 2025 08:09:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752505788; x=1753110588; 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=fJOIFm+8Lh6RSd39ar/6t/YbVeNymW5Iv03T46CT4Yw=; b=V/hjgysuLAfD6hx8bS2dPeJmOu4nUOMObvq447LObOR+Li1BU+nWUiX4AG+qWVh5rP fQEtp/48gSewqasmw4Nmc4ehYWkGbmTW/aFi1exFwL4oZd7zRQgyQN4LPfuZcpBxauMk I2S2NXBxUHb6hgnQyE6zBVj6IErEi/NNr9mynubrUYHiHDIzXCWZCQjPuCThiOyRlqBm V+b1zR2KTIlsvs/FTxvati8FdLmqfre9ZGsVY6a6szPF9hp0e8xwYsfIAjjRkJtdyGCW hUEBhbtb0oEBoktP9Qu1bhbCXKpet75srmN+yPwwbcBmzI5Tjl6ZcvxCI7xuFLeCkBeT 8TUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752505788; x=1753110588; 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=fJOIFm+8Lh6RSd39ar/6t/YbVeNymW5Iv03T46CT4Yw=; b=NZnahJSdh0LZzf1vPK+ThSTeoRD35eL2YRlHocddEqvmIMEf5tTcM5sGpIiw2lB2cV RZFXnCE9XIPLCwVOwF4ipq4/pkD5GvpMUpY9l/me1Z/juIzgFRMBAWAhuJ9beHjZyGi1 Qyr4dt/bh05Iy5i1RBr/YI/QMP45TTGeE5IRfZ0VTnmquTjiEljjwI+tcM3TSOZx9LEQ RFIDFEkTSGPxrs1sN5Gs8TYW7OSNnuW0UJ44M0iskfyEeqKGPXIIc6t2gP2xRa0UwMuU +ZfJ74+V3tXXxJ6lZzPMaD5YJmHOz6ka/UHNUghSQZx3GIPKZzwLBZPs6Hw4pkcRrxr6 fRKQ== X-Forwarded-Encrypted: i=1; AJvYcCVoVZJaj1uAgiZKkSv6zW8ocPLJ7kboBFwUVzoS7Mt/vQAhNW/73s2cHC2JaJwnQYO6E1Vll5X5Ml0=@vger.kernel.org X-Gm-Message-State: AOJu0YxPKvvbQ14BjWsWB4TDY4W/ILQCx0HHF3TAK9vp2lVe2nzMD0zJ FlHrHxk4EMRrKb/HFfMS+QjUv83uTJSVP3+71VQMtbX6hN9VNxpKY/mTGa9ei1xWKdUVBScSht4 +HwWZiGub7w== X-Google-Smtp-Source: AGHT+IEmubYECGBWdfe0DU+VaAT0zPkTkvt6Zg9ztBCJhb69BYC8pSS9D9DRVzRcXnXRT57XcoW6QDr+BH4R X-Received: from iovt22.prod.google.com ([2002:a05:6602:1416:b0:86c:fede:130b]) (user=jdenose job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6602:7408:b0:876:8790:6ac8 with SMTP id ca18e2360f4ac-87977f5f085mr1286207339f.1.1752505788704; Mon, 14 Jul 2025 08:09:48 -0700 (PDT) Date: Mon, 14 Jul 2025 15:09:43 +0000 In-Reply-To: <20250714-support-forcepads-v1-0-71c7c05748c9@google.com> Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250714-support-forcepads-v1-0-71c7c05748c9@google.com> X-Mailer: b4 0.14.2 Message-ID: <20250714-support-forcepads-v1-10-71c7c05748c9@google.com> Subject: [PATCH 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" 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 760dd1d70583489c07e199943ebba361d347bfa4..e83363cad6febb5d3fcd786b76e05bc16a7e4e94 100644 --- a/drivers/hid/hid-haptic.c +++ b/drivers/hid/hid-haptic.c @@ -5,6 +5,7 @@ * Copyright (c) 2021 Angela Czubak */ +#include #include #include "hid-haptic.h" @@ -197,12 +198,46 @@ static void fill_effect_buf(struct hid_haptic_device *haptic, mutex_unlock(&haptic->manual_trigger_mutex); } +static void switch_mode(struct hid_device *hdev, struct hid_haptic_device *haptic, + int mode) +{ + struct hid_report *rep = haptic->auto_trigger_report; + struct hid_field *field; + s32 value; + int i, j; + + if (mode == HID_HAPTIC_MODE_HOST) + value = HID_HAPTIC_ORDINAL_WAVEFORMSTOP; + else + value = haptic->default_auto_trigger; + + mutex_lock(&haptic->auto_trigger_mutex); + for (i = 0; i < rep->maxfield; i++) { + field = rep->field[i]; + /* Ignore if report count is out of bounds. */ + if (field->report_count < 1) + continue; + + for (j = 0; j < field->maxusage; j++) { + if (field->usage[j].hid == HID_HP_AUTOTRIGGER) + field->value[j] = value; + } + } + + /* send the report */ + hid_hw_request(hdev, rep, HID_REQ_SET_REPORT); + mutex_unlock(&haptic->auto_trigger_mutex); + haptic->mode = mode; +} + static int hid_haptic_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old) { + struct hid_device *hdev = input_get_drvdata(dev); struct ff_device *ff = dev->ff; struct hid_haptic_device *haptic = ff->private; int i, ordinal = 0; + bool switch_modes = false; /* If vendor range, check vendor id and page */ if (effect->u.hid.hid_usage >= (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.hid, &haptic->effect[effect->id], ordinal); + if (effect->u.hid.hid_usage == (HID_HP_WAVEFORMPRESS & HID_USAGE) || + effect->u.hid.hid_usage == (HID_HP_WAVEFORMRELEASE & HID_USAGE)) + switch_modes = true; + + /* If device is in autonomous mode, and the uploaded effect signals userspace + * wants control of the device, change modes + */ + if (switch_modes && haptic->mode == HID_HAPTIC_MODE_DEVICE) + switch_mode(hdev, haptic, HID_HAPTIC_MODE_HOST); + return 0; } @@ -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 = dev->ff->private; + struct hid_device *hdev = input_get_drvdata(dev); struct ff_effect effect; int ordinal; @@ -297,20 +343,25 @@ static int hid_haptic_erase(struct input_dev *dev, int effect_id) if (effect.u.hid.hid_usage == (HID_HP_WAVEFORMRELEASE & HID_USAGE)) { ordinal = haptic->release_ordinal; - if (!ordinal) + if (!ordinal) { ordinal = HID_HAPTIC_ORDINAL_WAVEFORMNONE; - else - effect.u.hid.hid_usage = HID_HP_WAVEFORMRELEASE & - HID_USAGE; + if (haptic->mode == HID_HAPTIC_MODE_HOST) + switch_mode(hdev, haptic, HID_HAPTIC_MODE_DEVICE); + } else { + effect.u.hid.hid_usage = HID_HP_WAVEFORMRELEASE & HID_USAGE; + } fill_effect_buf(haptic, &effect.u.hid, &haptic->effect[effect_id], ordinal); } else if (effect.u.hid.hid_usage == (HID_HP_WAVEFORMPRESS & HID_USAGE)) { ordinal = haptic->press_ordinal; - if (!ordinal) + if (!ordinal) { ordinal = HID_HAPTIC_ORDINAL_WAVEFORMNONE; + if (haptic->mode == HID_HAPTIC_MODE_HOST) + switch_mode(hdev, haptic, HID_HAPTIC_MODE_DEVICE); + } else - effect.u.hid.hid_usage = HID_HP_WAVEFORMPRESS & - HID_USAGE; + effect.u.hid.hid_usage = HID_HP_WAVEFORMPRESS & HID_USAGE; + fill_effect_buf(haptic, &effect.u.hid, &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] = HID_HP_WAVEFORMSTOP & HID_USAGE; + mutex_init(&haptic->auto_trigger_mutex); for (r = 0; r < haptic->auto_trigger_report->maxfield; r++) parse_auto_trigger_field(haptic, haptic->auto_trigger_report->field[r]); -- 2.50.0.727.gbf7dc18ff4-goog