OSDN Git Service

Input: allow drivers specify timestamp for input events
authorAtif Niyaz <atifniyaz@google.com>
Wed, 24 Jul 2019 19:26:31 +0000 (22:26 +0300)
committerDmitry Torokhov <dmitry.torokhov@gmail.com>
Thu, 25 Jul 2019 08:12:20 +0000 (11:12 +0300)
Currently, evdev stamps events with timestamps acquired in evdev_events()
However, this timestamping may not be accurate in terms of measuring
when the actual event happened.

Let's allow individual drivers specify timestamp in order to provide a more
accurate sense of time for the event. It is expected that drivers will set the
timestamp in their hard interrupt routine.

Signed-off-by: Atif Niyaz <atifniyaz@google.com>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
drivers/input/evdev.c
drivers/input/input.c
include/linux/input.h

index 867c2cf..d7dd6fc 100644 (file)
 #include <linux/cdev.h>
 #include "input-compat.h"
 
-enum evdev_clock_type {
-       EV_CLK_REAL = 0,
-       EV_CLK_MONO,
-       EV_CLK_BOOT,
-       EV_CLK_MAX
-};
-
 struct evdev {
        int open;
        struct input_handle handle;
@@ -53,7 +46,7 @@ struct evdev_client {
        struct fasync_struct *fasync;
        struct evdev *evdev;
        struct list_head node;
-       unsigned int clk_type;
+       enum input_clock_type clk_type;
        bool revoked;
        unsigned long *evmasks[EV_CNT];
        unsigned int bufsize;
@@ -149,17 +142,10 @@ static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)
 
 static void __evdev_queue_syn_dropped(struct evdev_client *client)
 {
+       ktime_t *ev_time = input_get_timestamp(client->evdev->handle.dev);
+       struct timespec64 ts = ktime_to_timespec64(ev_time[client->clk_type]);
        struct input_event ev;
-       ktime_t time;
-       struct timespec64 ts;
 
-       time = client->clk_type == EV_CLK_REAL ?
-                       ktime_get_real() :
-                       client->clk_type == EV_CLK_MONO ?
-                               ktime_get() :
-                               ktime_get_boottime();
-
-       ts = ktime_to_timespec64(time);
        ev.input_event_sec = ts.tv_sec;
        ev.input_event_usec = ts.tv_nsec / NSEC_PER_USEC;
        ev.type = EV_SYN;
@@ -188,18 +174,18 @@ static void evdev_queue_syn_dropped(struct evdev_client *client)
 static int evdev_set_clk_type(struct evdev_client *client, unsigned int clkid)
 {
        unsigned long flags;
-       unsigned int clk_type;
+       enum input_clock_type clk_type;
 
        switch (clkid) {
 
        case CLOCK_REALTIME:
-               clk_type = EV_CLK_REAL;
+               clk_type = INPUT_CLK_REAL;
                break;
        case CLOCK_MONOTONIC:
-               clk_type = EV_CLK_MONO;
+               clk_type = INPUT_CLK_MONO;
                break;
        case CLOCK_BOOTTIME:
-               clk_type = EV_CLK_BOOT;
+               clk_type = INPUT_CLK_BOOT;
                break;
        default:
                return -EINVAL;
@@ -307,12 +293,7 @@ static void evdev_events(struct input_handle *handle,
 {
        struct evdev *evdev = handle->private;
        struct evdev_client *client;
-       ktime_t ev_time[EV_CLK_MAX];
-
-       ev_time[EV_CLK_MONO] = ktime_get();
-       ev_time[EV_CLK_REAL] = ktime_mono_to_real(ev_time[EV_CLK_MONO]);
-       ev_time[EV_CLK_BOOT] = ktime_mono_to_any(ev_time[EV_CLK_MONO],
-                                                TK_OFFS_BOOT);
+       ktime_t *ev_time = input_get_timestamp(handle->dev);
 
        rcu_read_lock();
 
index 7f3c5fc..7494a0d 100644 (file)
@@ -1895,6 +1895,46 @@ void input_free_device(struct input_dev *dev)
 EXPORT_SYMBOL(input_free_device);
 
 /**
+ * input_set_timestamp - set timestamp for input events
+ * @dev: input device to set timestamp for
+ * @timestamp: the time at which the event has occurred
+ *   in CLOCK_MONOTONIC
+ *
+ * This function is intended to provide to the input system a more
+ * accurate time of when an event actually occurred. The driver should
+ * call this function as soon as a timestamp is acquired ensuring
+ * clock conversions in input_set_timestamp are done correctly.
+ *
+ * The system entering suspend state between timestamp acquisition and
+ * calling input_set_timestamp can result in inaccurate conversions.
+ */
+void input_set_timestamp(struct input_dev *dev, ktime_t timestamp)
+{
+       dev->timestamp[INPUT_CLK_MONO] = timestamp;
+       dev->timestamp[INPUT_CLK_REAL] = ktime_mono_to_real(timestamp);
+       dev->timestamp[INPUT_CLK_BOOT] = ktime_mono_to_any(timestamp,
+                                                          TK_OFFS_BOOT);
+}
+EXPORT_SYMBOL(input_set_timestamp);
+
+/**
+ * input_get_timestamp - get timestamp for input events
+ * @dev: input device to get timestamp from
+ *
+ * A valid timestamp is a timestamp of non-zero value.
+ */
+ktime_t *input_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;
+}
+EXPORT_SYMBOL(input_get_timestamp);
+
+/**
  * input_set_capability - mark device as capable of a certain event
  * @dev: device that is capable of emitting or accepting event
  * @type: type of the event (EV_KEY, EV_REL, etc...)
index 510e785..e95a439 100644 (file)
@@ -33,6 +33,13 @@ struct input_value {
        __s32 value;
 };
 
+enum input_clock_type {
+       INPUT_CLK_REAL = 0,
+       INPUT_CLK_MONO,
+       INPUT_CLK_BOOT,
+       INPUT_CLK_MAX
+};
+
 /**
  * struct input_dev - represents an input device
  * @name: name of the device
@@ -114,6 +121,8 @@ struct input_value {
  * @vals: array of values queued in the current frame
  * @devres_managed: indicates that devices is managed with devres framework
  *     and needs not be explicitly unregistered or freed.
+ * @timestamp: storage for a timestamp set by input_set_timestamp called
+ *  by a driver
  */
 struct input_dev {
        const char *name;
@@ -184,6 +193,8 @@ struct input_dev {
        struct input_value *vals;
 
        bool devres_managed;
+
+       ktime_t timestamp[INPUT_CLK_MAX];
 };
 #define to_input_dev(d) container_of(d, struct input_dev, dev)
 
@@ -382,6 +393,9 @@ void input_close_device(struct input_handle *);
 
 int input_flush_device(struct input_handle *handle, struct file *file);
 
+void input_set_timestamp(struct input_dev *dev, ktime_t timestamp);
+ktime_t *input_get_timestamp(struct input_dev *dev);
+
 void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value);
 void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value);