From patchwork Mon Jan 9 23:21:12 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 6132 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 4CA5F23E01 for ; Mon, 9 Jan 2012 23:21:37 +0000 (UTC) Received: from mail-bk0-f52.google.com (mail-bk0-f52.google.com [209.85.214.52]) by fiordland.canonical.com (Postfix) with ESMTP id 28B10A181FE for ; Mon, 9 Jan 2012 23:21:37 +0000 (UTC) Received: by bkbzu5 with SMTP id zu5so22354bkb.11 for ; Mon, 09 Jan 2012 15:21:36 -0800 (PST) Received: by 10.205.126.137 with SMTP id gw9mr7759283bkc.135.1326151296677; Mon, 09 Jan 2012 15:21:36 -0800 (PST) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.205.82.144 with SMTP id ac16cs68177bkc; Mon, 9 Jan 2012 15:21:36 -0800 (PST) Received: by 10.236.182.167 with SMTP id o27mr23255862yhm.115.1326151294214; Mon, 09 Jan 2012 15:21:34 -0800 (PST) Received: from e5.ny.us.ibm.com (e5.ny.us.ibm.com. [32.97.182.145]) by mx.google.com with ESMTPS id d50si66990927yhh.81.2012.01.09.15.21.33 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 09 Jan 2012 15:21:34 -0800 (PST) Received-SPF: pass (google.com: domain of jstultz@us.ibm.com designates 32.97.182.145 as permitted sender) client-ip=32.97.182.145; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jstultz@us.ibm.com designates 32.97.182.145 as permitted sender) smtp.mail=jstultz@us.ibm.com Received: from /spool/local by e5.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 9 Jan 2012 18:21:33 -0500 Received: from d01relay04.pok.ibm.com (9.56.227.236) by e5.ny.us.ibm.com (192.168.1.105) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 9 Jan 2012 18:21:31 -0500 Received: from d03av02.boulder.ibm.com (d03av02.boulder.ibm.com [9.17.195.168]) by d01relay04.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q09NLUla340508; Mon, 9 Jan 2012 18:21:31 -0500 Received: from d03av02.boulder.ibm.com (loopback [127.0.0.1]) by d03av02.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q09NLS7m022686; Mon, 9 Jan 2012 16:21:30 -0700 Received: from kernel.beaverton.ibm.com (kernel.beaverton.ibm.com [9.47.67.96]) by d03av02.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q09NLQDU022585; Mon, 9 Jan 2012 16:21:26 -0700 Received: by kernel.beaverton.ibm.com (Postfix, from userid 1056) id 838071E74FB; Mon, 9 Jan 2012 15:21:25 -0800 (PST) From: John Stultz To: linux-input@vger.kernel.org Cc: John Stultz , Dmitry Torokhov , Daniel Kurtz , =?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?= , Brian Swetland , Colin Cross , Dima Zavin Subject: [PATCH] Input: Add infrastrucutre for selecting clockid for event time stamps. Date: Mon, 9 Jan 2012 15:21:12 -0800 Message-Id: <1326151272-29507-1-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.7.3.2.146.gca209 MIME-Version: 1.0 x-cbid: 12010923-5930-0000-0000-000003F1ECD6 Ok. Now that Android and ChromeOS folks have commented on it, I wanted to go ahead and submit this for inclusion. The only change from the previous version is that it includes Danial's suggested ktime_to_timeval() simplificiation. Let me know if there are any other thoughts/comments. thanks -john As noted by Arve and others, since wall time can jump backwards, it is difficult to use for input because one cannot determine if one event occured before another or for how long a key was pressed. However, the timestamp field is part of the kernel ABI, and cannot be changed without possibly breaking existing users. This patch adds a new IOCTL that allows a clockid to be set in the evdev_client struct that will specify which time base to use for event timestamps (ie: CLOCK_MONOTONIC instead of CLOCK_REALTIME). For now we only support CLOCK_MONOTONIC and CLOCK_REALTIME, but in the future we could support other clockids if appropriate. The default remains CLOCK_REALTIME, so we don't change the ABI. CC: Dmitry Torokhov CC: Daniel Kurtz CC: linux-input@vger.kernel.org CC: Arve Hjønnevåg CC: Brian Swetland CC: Colin Cross CC: Dima Zavin Reviewed-by: Daniel Kurtz Signed-off-by: John Stultz --- drivers/input/evdev.c | 30 ++++++++++++++++++++++++++---- include/linux/input.h | 2 ++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/drivers/input/evdev.c b/drivers/input/evdev.c index 4cf2534..de8f8f1 100644 --- a/drivers/input/evdev.c +++ b/drivers/input/evdev.c @@ -46,6 +46,7 @@ struct evdev_client { struct fasync_struct *fasync; struct evdev *evdev; struct list_head node; + int timestamp_clkid; unsigned int bufsize; struct input_event buffer[]; }; @@ -54,8 +55,16 @@ static struct evdev *evdev_table[EVDEV_MINORS]; static DEFINE_MUTEX(evdev_table_mutex); static void evdev_pass_event(struct evdev_client *client, - struct input_event *event) + struct input_event *event, + ktime_t mono, ktime_t real) { + struct timespec ts; + + if (client->timestamp_clkid == CLOCK_MONOTONIC) + event->time = ktime_to_timeval(mono); + else + event->time = ktime_to_timeval(real); + /* Interrupts are disabled, just acquire the lock. */ spin_lock(&client->buffer_lock); @@ -94,8 +103,11 @@ static void evdev_event(struct input_handle *handle, struct evdev *evdev = handle->private; struct evdev_client *client; struct input_event event; + ktime_t time_mono, time_real; + + time_mono = ktime_get(); + time_real = ktime_sub(time_mono, ktime_get_monotonic_offset()); - do_gettimeofday(&event.time); event.type = type; event.code = code; event.value = value; @@ -103,11 +115,12 @@ static void evdev_event(struct input_handle *handle, rcu_read_lock(); client = rcu_dereference(evdev->grab); + if (client) - evdev_pass_event(client, &event); + evdev_pass_event(client, &event, time_mono, time_real); else list_for_each_entry_rcu(client, &evdev->client_list, node) - evdev_pass_event(client, &event); + evdev_pass_event(client, &event, time_mono, time_real); rcu_read_unlock(); @@ -683,6 +696,15 @@ static long evdev_do_ioctl(struct file *file, unsigned int cmd, else return evdev_ungrab(evdev, client); + case EVIOCCLOCKID: + if (copy_from_user(&i, p, sizeof(unsigned int))) + return -EFAULT; + if ((i == CLOCK_MONOTONIC) || (i == CLOCK_REALTIME)) { + client->timestamp_clkid = i; + return 0; + } + return -EINVAL; + case EVIOCGKEYCODE: return evdev_handle_get_keycode(dev, p); diff --git a/include/linux/input.h b/include/linux/input.h index 3862e32..9618e14 100644 --- a/include/linux/input.h +++ b/include/linux/input.h @@ -129,6 +129,8 @@ struct input_keymap_entry { #define EVIOCGRAB _IOW('E', 0x90, int) /* Grab/Release device */ +#define EVIOCCLOCKID _IOW('E', 0xA0, int) /* Set clockid to be used for timestamps */ + /* * Device properties and quirks */