From patchwork Tue Sep 13 14:10:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepa Dinamani X-Patchwork-Id: 9329223 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9C13560231 for ; Tue, 13 Sep 2016 14:11:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8D825294F4 for ; Tue, 13 Sep 2016 14:11:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 82749294F7; Tue, 13 Sep 2016 14:11:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 11E3E294F4 for ; Tue, 13 Sep 2016 14:11:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758587AbcIMOLL (ORCPT ); Tue, 13 Sep 2016 10:11:11 -0400 Received: from mail-pa0-f66.google.com ([209.85.220.66]:35334 "EHLO mail-pa0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758393AbcIMOKm (ORCPT ); Tue, 13 Sep 2016 10:10:42 -0400 Received: by mail-pa0-f66.google.com with SMTP id pp5so9267635pac.2; Tue, 13 Sep 2016 07:10:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=yM74RH1qgyMp6ekLYOHwd9C5hDPLoy4fQXiyhHxJou4=; b=L/flOa9f4Xqzav4LXDIz1wghWduouws9ipOsT8NwSjPuQEaprFWNIbV/+D1sYu3jSG BIRRzBEx+amuBMPI+crn7RMD2K6JjrBtwXrQ+c6Spqpu4OKi0Im1lBJTkkne21LK2wC0 J0OOorNhxNFhrnHh1a+Emb3JvlANejqRXWoRijWknsTiPnmLj2YlDNWcdmqwxT0xVNIg MM/PSN+gmMbvytCZR4rpL3i9XkM+g3KQkvBucOJebpas8tg6bTfY5NlKeM/j+6L19ij6 Xb3MVNHjvyADTP9wx+2awG4A5/mPGh5wZeDukeh7lIaitQIP7YZGjF417e4nN2Bzs6al P65Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=yM74RH1qgyMp6ekLYOHwd9C5hDPLoy4fQXiyhHxJou4=; b=Y4WahdHpD3xG+ZaaapX20wgAPtFOT94Lj+FpeY5kVkd4uE67oDmGUR9QaYyI9dvhdM WyK54zIHy0SCshhGveFC6YmtUdGOC/Ye1vjdIr5TQw99aLzPnL9KJrsKnxF3qgUkCI0E EC9IAglUxkB44GRY+2bulL0u/k11EbJbuuLkeIYTTMAdGmV+tFO2v4S7NCQXh44Ms7b0 jf0lS8H+HhldvoCLzVJfQsrcjGqmJfNrlcfTfIkUYAMn2JR48r+wTfKbHAmyX8Mijy6f e+LVMrX6S/kAc1PXQlQtq+p8pL+hDjXJzj7yddh6gPF6YH+LEtwPpUJiSWMaGv25nPNe M3rA== X-Gm-Message-State: AE9vXwOq/JaCdWDWIQnPEH2b4XXJEKG+ieONsEYOQCfxAwv8aKvNcMCgkpudLZFzZTa6JA== X-Received: by 10.66.43.164 with SMTP id x4mr1679635pal.11.1473775836841; Tue, 13 Sep 2016 07:10:36 -0700 (PDT) Received: from deepa-ubuntu.hsd1.ca.comcast.net ([2601:647:5000:6620:d498:fb30:692a:b2f2]) by smtp.gmail.com with ESMTPSA id dh16sm11968193pac.4.2016.09.13.07.10.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 13 Sep 2016 07:10:36 -0700 (PDT) From: Deepa Dinamani To: Dmitry Torokhov , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Cc: arnd@arndb.de, y2038@lists.linaro.org Subject: [PATCH 1/4] uinput: Add ioctl for using monotonic/ boot times Date: Tue, 13 Sep 2016 07:10:02 -0700 Message-Id: <1473775805-2242-2-git-send-email-deepa.kernel@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1473775805-2242-1-git-send-email-deepa.kernel@gmail.com> References: <1473775805-2242-1-git-send-email-deepa.kernel@gmail.com> Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP struct timeval which is part of struct input_event to maintain the event times is not y2038 safe. Real time timestamps are also not ideal for input_event as this time can go backwards as noted in the patch a80b83b7b8 by John Stultz. Arnd Bergmann suggested deprecating real time and using monotonic or other timers for all input_event times as a solution to both the above problems. Add a new ioctl to let the user dictate the kind of time to be used for input events. This is similar to the evdev implementation of the feature. Realtime is still the default time. This is to maintain backward compatibility. The structure to maintain input events will be changed in a different patch. Signed-off-by: Deepa Dinamani --- drivers/input/misc/uinput.c | 58 ++++++++++++++++++++++++++++++++++++++++++++- include/linux/uinput.h | 1 + include/uapi/linux/uinput.h | 3 +++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/drivers/input/misc/uinput.c b/drivers/input/misc/uinput.c index 92595b9..ae22927 100644 --- a/drivers/input/misc/uinput.c +++ b/drivers/input/misc/uinput.c @@ -46,11 +46,28 @@ static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) { struct uinput_device *udev = input_get_drvdata(dev); + struct timespec64 ts; + ktime_t kt; udev->buff[udev->head].type = type; udev->buff[udev->head].code = code; udev->buff[udev->head].value = value; - do_gettimeofday(&udev->buff[udev->head].time); + + kt = ktime_get(); + switch (udev->clk_type) { + case CLOCK_REALTIME: + ts = ktime_to_timespec64(ktime_mono_to_real(kt)); + break; + case CLOCK_MONOTONIC: + ts = ktime_to_timespec64(kt); + break; + case CLOCK_BOOTTIME: + ts = ktime_to_timespec64(ktime_mono_to_any(kt, TK_OFFS_BOOT)); + break; + } + + udev->buff[udev->head].time.tv_sec = ts.tv_sec; + udev->buff[udev->head].time.tv_usec = ts.tv_nsec / NSEC_PER_USEC; udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE; wake_up_interruptible(&udev->waitq); @@ -295,6 +312,7 @@ static int uinput_create_device(struct uinput_device *udev) if (error) goto fail2; + udev->clk_type = CLOCK_REALTIME; udev->state = UIST_CREATED; return 0; @@ -304,6 +322,38 @@ static int uinput_create_device(struct uinput_device *udev) return error; } +static int uinput_set_clk_type(struct uinput_device *udev, unsigned int clkid) +{ + unsigned int clk_type; + + if (udev->state != UIST_CREATED) + return -EINVAL; + + switch (clkid) { + /* Realtime clock is only valid until year 2038.*/ + case CLOCK_REALTIME: + clk_type = CLOCK_REALTIME; + break; + case CLOCK_MONOTONIC: + clk_type = CLOCK_MONOTONIC; + break; + case CLOCK_BOOTTIME: + clk_type = CLOCK_BOOTTIME; + break; + default: + return -EINVAL; + } + + if (udev->clk_type != clk_type) { + udev->clk_type = clk_type; + + /* Flush pending events */ + uinput_flush_requests(udev); + } + + return 0; +} + static int uinput_open(struct inode *inode, struct file *file) { struct uinput_device *newdev; @@ -787,6 +837,7 @@ static long uinput_ioctl_handler(struct file *file, unsigned int cmd, char *phys; const char *name; unsigned int size; + int clock_id; retval = mutex_lock_interruptible(&udev->mutex); if (retval) @@ -817,6 +868,11 @@ static long uinput_ioctl_handler(struct file *file, unsigned int cmd, retval = uinput_dev_setup(udev, p); goto out; + case UI_SET_CLOCKID: + if (copy_from_user(&clock_id, p, sizeof(unsigned int))) + return -EFAULT; + return uinput_set_clk_type(udev, clock_id); + /* UI_ABS_SETUP is handled in the variable size ioctls */ case UI_SET_EVBIT: diff --git a/include/linux/uinput.h b/include/linux/uinput.h index 75de43d..6527fb7 100644 --- a/include/linux/uinput.h +++ b/include/linux/uinput.h @@ -72,6 +72,7 @@ struct uinput_device { unsigned char head; unsigned char tail; struct input_event buff[UINPUT_BUFFER_SIZE]; + int clk_type; unsigned int ff_effects_max; struct uinput_request *requests[UINPUT_NUM_REQUESTS]; diff --git a/include/uapi/linux/uinput.h b/include/uapi/linux/uinput.h index dc652e2..d9494ae 100644 --- a/include/uapi/linux/uinput.h +++ b/include/uapi/linux/uinput.h @@ -133,6 +133,9 @@ struct uinput_abs_setup { */ #define UI_ABS_SETUP _IOW(UINPUT_IOCTL_BASE, 4, struct uinput_abs_setup) +/* Set clockid to be used for timestamps */ +#define UI_SET_CLOCKID _IOW(UINPUT_IOCTL_BASE, 5, int) + #define UI_SET_EVBIT _IOW(UINPUT_IOCTL_BASE, 100, int) #define UI_SET_KEYBIT _IOW(UINPUT_IOCTL_BASE, 101, int) #define UI_SET_RELBIT _IOW(UINPUT_IOCTL_BASE, 102, int)