All of lore.kernel.org
 help / color / mirror / Atom feed
From: Angela Czubak <acz@semihalf.com>
To: linux-input@vger.kernel.org
Cc: upstream@semihalf.com, dmitry.torokhov@gmail.com,
	Angela Czubak <acz@semihalf.com>
Subject: [PATCH 14/18] HID: haptic: add hid_haptic_switch_mode
Date: Tue, 21 Dec 2021 19:17:39 +0000	[thread overview]
Message-ID: <20211221191743.1893185-15-acz@semihalf.com> (raw)
In-Reply-To: <20211221191743.1893185-1-acz@semihalf.com>

Function hid_haptic_switch_mode() can be used to turn off and on the
autonomoums mode for the device. If the device supports press and release
waveforms, let the kernel handle generation of haptic feedback instead of
the device itself.
Implement hid_haptic_resume() and hid_haptic_suspend() so that the
autonomous mode gets switched off at resume and switched on at suspend.

Signed-off-by: Angela Czubak <acz@semihalf.com>
---
 drivers/hid/hid-haptic.c | 89 +++++++++++++++++++++++++++++++++++++---
 drivers/hid/hid-haptic.h | 10 +++++
 2 files changed, 93 insertions(+), 6 deletions(-)

diff --git a/drivers/hid/hid-haptic.c b/drivers/hid/hid-haptic.c
index 85c4711f685e..e7fd53d1a7c1 100644
--- a/drivers/hid/hid-haptic.c
+++ b/drivers/hid/hid-haptic.c
@@ -8,6 +8,7 @@
 /*
  */
 
+#include <linux/input/mt.h>
 #include <linux/module.h>
 
 #include "hid-haptic.h"
@@ -200,9 +201,61 @@ 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_KERNEL) {
+		value = HID_HAPTIC_ORDINAL_WAVEFORMSTOP;
+		input_mt_pressure_toggle(haptic->input_dev, 0);
+	} else {
+		value = haptic->default_auto_trigger;
+		input_mt_pressure_toggle(haptic->input_dev, 1);
+	}
+
+	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;
+}
+
+#ifdef CONFIG_PM
+void hid_haptic_resume(struct hid_device *hdev, struct hid_haptic_device *haptic)
+{
+	if (haptic->press_ordinal_cur && haptic->release_ordinal_cur)
+		switch_mode(hdev, haptic, HID_HAPTIC_MODE_KERNEL);
+}
+EXPORT_SYMBOL_GPL(hid_haptic_resume);
+
+void hid_haptic_suspend(struct hid_device *hdev, struct hid_haptic_device *haptic)
+{
+	if (haptic->press_ordinal_cur && haptic->release_ordinal_cur)
+		switch_mode(hdev, haptic, HID_HAPTIC_MODE_DEVICE);
+}
+EXPORT_SYMBOL_GPL(hid_haptic_suspend);
+#endif
+
 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;
@@ -228,6 +281,20 @@ 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->id == HID_HAPTIC_RELEASE_EFFECT_ID) {
+		if (haptic->press_ordinal_cur &&
+		    haptic->mode == HID_HAPTIC_MODE_DEVICE) {
+			switch_mode(hdev, haptic, HID_HAPTIC_MODE_KERNEL);
+		}
+		haptic->release_ordinal_cur = ordinal;
+	} else if (effect->id == HID_HAPTIC_PRESS_EFFECT_ID) {
+		if (haptic->release_ordinal_cur &&
+		    haptic->mode == HID_HAPTIC_MODE_DEVICE) {
+			switch_mode(hdev, haptic, HID_HAPTIC_MODE_KERNEL);
+		}
+		haptic->press_ordinal_cur = ordinal;
+	}
+
 	return 0;
 }
 
@@ -294,6 +361,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;
 
@@ -301,21 +369,29 @@ static int hid_haptic_erase(struct input_dev *dev, int effect_id)
 	switch (effect_id) {
 	case HID_HAPTIC_RELEASE_EFFECT_ID:
 		ordinal = haptic->release_ordinal_orig;
-		if (!ordinal)
+		haptic->release_ordinal_cur = ordinal;
+		if (!ordinal) {
 			ordinal = HID_HAPTIC_ORDINAL_WAVEFORMNONE;
-		else
+			if (haptic->mode == HID_HAPTIC_MODE_KERNEL)
+				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);
 		break;
 	case HID_HAPTIC_PRESS_EFFECT_ID:
 		ordinal = haptic->press_ordinal_orig;
-		if (!ordinal)
+		haptic->press_ordinal_cur = ordinal;
+		if (!ordinal) {
 			ordinal = HID_HAPTIC_ORDINAL_WAVEFORMNONE;
-		else
+			if (haptic->mode == HID_HAPTIC_MODE_KERNEL)
+				switch_mode(hdev, haptic, HID_HAPTIC_MODE_DEVICE);
+		} else {
 			effect.u.hid.hid_usage = HID_HP_WAVEFORMPRESS &
 				HID_USAGE;
+		}
 		fill_effect_buf(haptic, &effect.u.hid, &haptic->effect[effect_id],
 				ordinal);
 		break;
@@ -408,6 +484,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]);
 
@@ -583,13 +660,13 @@ void hid_haptic_handle_press_release(struct hid_haptic_device *haptic)
 	if (!prev_pressed_state && haptic->pressed_state &&
 	    haptic->mode == HID_HAPTIC_MODE_KERNEL) {
 		spin_lock_irqsave(&input->event_lock, flags);
-		input->ff->playback(input, PRESS_HID_EFFECT_ID, 1);
+		input->ff->playback(input, HID_HAPTIC_PRESS_EFFECT_ID, 1);
 		spin_unlock_irqrestore(&input->event_lock, flags);
 	}
 	if (prev_pressed_state && !haptic->pressed_state &&
 	    haptic->mode == HID_HAPTIC_MODE_KERNEL) {
 		spin_lock_irqsave(&input->event_lock, flags);
-		input->ff->playback(input, RELEASE_HID_EFFECT_ID, 1);
+		input->ff->playback(input, HID_HAPTIC_RELEASE_EFFECT_ID, 1);
 		spin_unlock_irqrestore(&input->event_lock, flags);
 	}
 }
diff --git a/drivers/hid/hid-haptic.h b/drivers/hid/hid-haptic.h
index 27ae1ed576c4..7a4571075a21 100644
--- a/drivers/hid/hid-haptic.h
+++ b/drivers/hid/hid-haptic.h
@@ -82,6 +82,10 @@ int hid_haptic_input_mapping(struct hid_device *hdev,
 int hid_haptic_input_configured(struct hid_device *hdev,
 				struct hid_haptic_device *haptic,
 				struct hid_input *hi);
+#ifdef CONFIG_PM
+void hid_haptic_resume(struct hid_device *hdev, struct hid_haptic_device *haptic);
+void hid_haptic_suspend(struct hid_device *hdev, struct hid_haptic_device *haptic);
+#endif
 int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device **haptic_ptr);
 void hid_haptic_handle_press_release(struct hid_haptic_device *haptic);
 bool hid_haptic_handle_input(struct hid_haptic_device *haptic);
@@ -117,6 +121,12 @@ int hid_haptic_input_configured(struct hid_device *hdev,
 {
 	return 0;
 }
+#ifdef CONFIG_PM
+static inline
+void hid_haptic_resume(struct hid_device *hdev, struct hid_haptic_device *haptic) {}
+static inline
+void hid_haptic_suspend(struct hid_device *hdev, struct hid_haptic_device *haptic) {}
+#endif
 static inline
 int hid_haptic_init(struct hid_device *hdev, struct hid_haptic_device **haptic_ptr)
 {
-- 
2.34.1.307.g9b7440fafd-goog


  parent reply	other threads:[~2021-12-21 19:17 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-21 19:17 [PATCH 00/18] *** Implement simple haptic HID support *** Angela Czubak
2021-12-21 19:17 ` [PATCH 01/18] HID: add haptics page defines Angela Czubak
2022-01-07 21:40   ` Dmitry Torokhov
2021-12-21 19:17 ` [PATCH 02/18] Input: add FF_HID effect type Angela Czubak
2021-12-30 16:41   ` Harry Cutts
2021-12-21 19:17 ` [PATCH 03/18] Input: add INPUT_PROP_HAPTIC_TOUCHPAD Angela Czubak
2022-01-07 21:43   ` Dmitry Torokhov
2021-12-21 19:17 ` [PATCH 04/18] HID: haptic: introduce hid_haptic_device Angela Czubak
2022-01-07 22:18   ` Dmitry Torokhov
2022-01-10 19:42     ` Angela Czubak
2021-12-21 19:17 ` [PATCH 05/18] HID: introduce hid_get_feature Angela Czubak
2022-01-07 22:01   ` Dmitry Torokhov
2022-01-10 19:43     ` Angela Czubak
2022-01-12  9:43       ` Benjamin Tissoires
2022-01-12 11:26         ` Angela Czubak
2022-01-13  9:54           ` Benjamin Tissoires
2022-01-14 18:24             ` Angela Czubak
2022-01-17 10:08               ` Benjamin Tissoires
2021-12-21 19:17 ` [PATCH 06/18] HID: haptic: add functions for mapping and configuration Angela Czubak
2021-12-21 19:17 ` [PATCH 07/18] HID: input: allow mapping of haptic output Angela Czubak
2022-01-07 21:58   ` Dmitry Torokhov
2021-12-21 19:17 ` [PATCH 08/18] HID: haptic: initialize haptic device Angela Czubak
2021-12-21 19:17 ` [PATCH 09/18] Input: add shared effects Angela Czubak
2021-12-21 19:17 ` [PATCH 10/18] HID: haptic: implement release and press effects Angela Czubak
2021-12-21 19:17 ` [PATCH 11/18] HID: input: calculate resolution for pressure Angela Czubak
2022-01-07 22:23   ` Dmitry Torokhov
2021-12-21 19:17 ` [PATCH 12/18] HID: haptic: add functions handling events Angela Czubak
2021-12-21 19:17 ` [PATCH 13/18] Input: MT - toggle ABS_PRESSURE pointer emulation Angela Czubak
2022-01-07 22:07   ` Dmitry Torokhov
2022-01-10 19:43     ` Angela Czubak
2022-01-10 21:02       ` Dmitry Torokhov
2022-01-11 17:06         ` Angela Czubak
2022-01-12  2:19           ` Sean O'Brien
2022-01-12  2:52             ` Dmitry Torokhov
2022-01-12  9:02               ` Angela Czubak
2022-01-12  9:17               ` Benjamin Tissoires
2022-01-14 18:26                 ` Angela Czubak
2022-01-21  6:10               ` Peter Hutterer
2022-01-25 16:56                 ` Angela Czubak
2022-01-28  5:25                   ` Peter Hutterer
2021-12-21 19:17 ` Angela Czubak [this message]
2021-12-21 19:17 ` [PATCH 15/18] HID: multitouch: add haptic multitouch support Angela Czubak
2021-12-21 19:17 ` [PATCH 16/18] Input: introduce EVIOCFF(TAKE|RELEASE)CONTROL Angela Czubak
2021-12-21 19:17 ` [PATCH 17/18] HID: haptic: add hid_haptic_change_control Angela Czubak
2021-12-21 19:17 ` [PATCH 18/18] HID: i2c-hid: fix i2c_hid_set_or_send_report Angela Czubak
2022-01-08  6:46   ` Dmitry Torokhov
2022-01-10 19:43     ` Angela Czubak
2021-12-22 16:17 ` [PATCH 00/18] *** Implement simple haptic HID support *** Roderick Colenbrander

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211221191743.1893185-15-acz@semihalf.com \
    --to=acz@semihalf.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=upstream@semihalf.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.