PETC: Generate synthetic timestamp immediately

Generate a synthetic timestamp immediately when an input
event received from a source device has an invalid timestamp.

Bug: b/337877439
Test: Manual
Change-Id: I0651a340c531a478f3a0136e8d77de07a35a0e2c
diff --git a/petc_input_filter/petc_input_filter.c b/petc_input_filter/petc_input_filter.c
index 16468ff..76f9075 100644
--- a/petc_input_filter/petc_input_filter.c
+++ b/petc_input_filter/petc_input_filter.c
@@ -82,7 +82,7 @@
 static bool petc_if_gating_wheel_events(struct petc_if_drv_data *drv_data,
 					ktime_t timestamp)
 {
-	if (timestamp < drv_data->wheel_event_gate_end_time) {
+	if (ktime_compare(timestamp, drv_data->wheel_event_gate_end_time) < 0) {
 			pr_debug("[PETC_IF] Gating events\n");
 			return true;
 	}
@@ -426,10 +426,26 @@
 	}
 }
 
+/*
+ * This is similar to input_get_timestamp(), except that it returns the
+ * INPUT_CLK_MONO timestamp instead of INPUT_CLK_REAL.
+ * The monotonic timestamp is needed for use with input_set_timestamp().
+ */
+static ktime_t *petc_if_get_timestamp(struct input_dev *dev)
+{
+	const ktime_t invalid_timestamp = ktime_set(0, 0);
+
+	if (!ktime_compare(dev->timestamp[INPUT_CLK_MONO], invalid_timestamp))
+		input_set_timestamp(dev, ktime_get());
+
+	return &dev->timestamp[INPUT_CLK_MONO];
+}
+
 static bool petc_if_filter(struct input_handle *handle, unsigned int type,
 			   unsigned int code, int value)
 {
 	struct petc_if_dev_data *dev_data = handle->private;
+	ktime_t *timestamp = petc_if_get_timestamp(handle->dev);
 
 	if (!dev_data->clone_registered) {
 		pr_debug("[PETC_IF] input received but clone not yet registered\n");
@@ -437,7 +453,7 @@
 	}
 
 	return petc_if_handle_input(dev_data,
-				    handle->dev->timestamp[INPUT_CLK_MONO],
+				    *timestamp,
 				    type, code, value);
 }