From patchwork Mon Dec 4 00:55:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepa Dinamani X-Patchwork-Id: 10089499 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 4D93160327 for ; Mon, 4 Dec 2017 00:57:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2EECF28D5D for ; Mon, 4 Dec 2017 00:57:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2404028D61; Mon, 4 Dec 2017 00:57:27 +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 BA8AA28D5D for ; Mon, 4 Dec 2017 00:57:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752902AbdLDA4d (ORCPT ); Sun, 3 Dec 2017 19:56:33 -0500 Received: from mail-pf0-f193.google.com ([209.85.192.193]:40355 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752895AbdLDA4c (ORCPT ); Sun, 3 Dec 2017 19:56:32 -0500 Received: by mail-pf0-f193.google.com with SMTP id v26so7284843pfl.7; Sun, 03 Dec 2017 16:56:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=wRoP874NG3gPUxKio8cWzySPw/fcm2LwS6+UpIPQvQI=; b=pE0Wgk3A4ej11mGWp94yUVgSIkwZ3nRl9mrBscsfe67CKAJNjAceSS19GGfkpqay8G kIJBsflQ4eJV6gwSbV7xHpoMx+uXsrqOOQ0sQgnAru/uRtaQNSNU3wSgvjB4CkaJU2JK /MGJlao02wf7zFl0sc1cZjbmukYIqMiaOaWX9bQWL4jyyoTRcy0nerSYa9A77EmxnqXS hbV9RTO19YGrVkgXdhcKSNnxzSEa2HZl/iHTnh/E+ihmDMhxaxegc7OxZ5V+xx7SX9m4 dj8xk3zbVNeNIgdZhDNckqMvYxFjy8AAINtwGzomDRlphywN7dCL0E7MNZ0uARTan/+6 lxTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=wRoP874NG3gPUxKio8cWzySPw/fcm2LwS6+UpIPQvQI=; b=t/DqFLJYiDPyaeDl8Fpsyrbt6E0WT+PnbX3UGL1DlfLwfZnYVC6BzX8SKbge8nt5NX SEdkNxGjpvfufyuaiMM+OXOUmeezvFlDwrIKsTR/nT3Mq6DeRlIR00kDkhcKw2tQsMVR nHNEZI6REQXBZZ66DP64OdWOUKEi68P4HZgdRku5Ha4e+F2uN9928ZKfpG47kxjhm8YI /LsM3S3XMtiymlwb+H1DopYynVYFL6TSAQPZjBHNH6lEn8chzqmMNrhiP5+fJ3FdkjkM XacNCbzgJfmh/UTuYIj11ZQY5e50I5Tk1UfYCVStu8QqcJHx/H/oLkF7PHolvJIjU39j lmJA== X-Gm-Message-State: AJaThX4vrsUNVnG6xlQLnSKbD49lxYg0akUNfN4RBhCdw66Z8AMZ1ji2 mJ71ppQBR/2ezW7WTRwIWnc= X-Google-Smtp-Source: AGs4zMb5dTLcdK/cWsIHun0HVaDxLke3xazyVOjGlpE9zzQLiTPXRGz4YXhNQwa0rxsq9iyMrIfOXg== X-Received: by 10.98.159.135 with SMTP id v7mr16911684pfk.162.1512348991942; Sun, 03 Dec 2017 16:56:31 -0800 (PST) Received: from deepa-ubuntu.hsd1.ca.comcast.net ([2601:647:5000:6620:3092:62ad:3bb4:62e5]) by smtp.gmail.com with ESMTPSA id b67sm22056121pfm.19.2017.12.03.16.56.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 03 Dec 2017 16:56:31 -0800 (PST) From: Deepa Dinamani To: dmitry.torokhov@gmail.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Cc: arnd@arndb.de, y2038@lists.linaro.org Subject: [PATCH v3 1/4] uinput: Add ioctl for using monotonic/ boot times Date: Sun, 3 Dec 2017 16:55:42 -0800 Message-Id: <20171204005545.23325-2-deepa.kernel@gmail.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20171204005545.23325-1-deepa.kernel@gmail.com> References: <20171204005545.23325-1-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 | 57 ++++++++++++++++++++++++++++++++++++++++++++- include/uapi/linux/uinput.h | 3 +++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/drivers/input/misc/uinput.c b/drivers/input/misc/uinput.c index 39ddd9a73feb..5a450dc04a94 100644 --- a/drivers/input/misc/uinput.c +++ b/drivers/input/misc/uinput.c @@ -74,6 +74,7 @@ struct uinput_device { unsigned char tail; struct input_event buff[UINPUT_BUFFER_SIZE]; unsigned int ff_effects_max; + unsigned int clk_type; struct uinput_request *requests[UINPUT_NUM_REQUESTS]; wait_queue_head_t requests_waitq; @@ -84,11 +85,26 @@ 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; 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); + + switch (udev->clk_type) { + case CLOCK_REALTIME: + ktime_get_real_ts64(&ts); + break; + case CLOCK_MONOTONIC: + ktime_get_ts64(&ts); + break; + case CLOCK_BOOTTIME: + get_monotonic_boottime64(&ts); + 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); @@ -370,6 +386,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; @@ -379,6 +396,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; @@ -850,6 +899,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) @@ -881,6 +931,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/uapi/linux/uinput.h b/include/uapi/linux/uinput.h index c9e677e3af1d..78b480d1b6c3 100644 --- a/include/uapi/linux/uinput.h +++ b/include/uapi/linux/uinput.h @@ -134,6 +134,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)