Input: evdev - Add ioctl to block suspend while event queue is not empty.
authorArve Hjønnevåg <arve@android.com>
Fri, 17 Oct 2008 22:20:55 +0000 (15:20 -0700)
committerArve Hjønnevåg <arve@android.com>
Mon, 1 Jul 2013 20:40:19 +0000 (13:40 -0700)
Add an ioctl, EVIOCSSUSPENDBLOCK, to enable a wakelock that will block
suspend while the event queue is not empty. This allows userspace code to
process input events while the device appears to be asleep.

The current code holds the wakelock for up 5 seconds for every input
device and client. This can prevent suspend if sensor with a high data
rate is active, even when that sensor is not capable of waking the
device once it is suspended.

Change-Id: I624d66ef30a0b3abb543685c343382b8419b42b9
Signed-off-by: Arve Hjønnevåg <arve@android.com>
drivers/input/evdev.c
include/uapi/linux/input.h

index f0f8928b3c8a2902295ccfdf4803b43e00f3a0c9..23425d7922ca0240f0527306a49e23540f99c9c9 100644 (file)
@@ -24,6 +24,7 @@
 #include <linux/major.h>
 #include <linux/device.h>
 #include <linux/cdev.h>
+#include <linux/wakelock.h>
 #include "input-compat.h"
 
 struct evdev {
@@ -44,6 +45,9 @@ struct evdev_client {
        unsigned int tail;
        unsigned int packet_head; /* [future] position of the first element of next packet */
        spinlock_t buffer_lock; /* protects access to buffer, head and tail */
+       struct wake_lock wake_lock;
+       bool use_wake_lock;
+       char name[28];
        struct fasync_struct *fasync;
        struct evdev *evdev;
        struct list_head node;
@@ -71,10 +75,14 @@ static void __pass_event(struct evdev_client *client,
                client->buffer[client->tail].value = 0;
 
                client->packet_head = client->tail;
+               if (client->use_wake_lock)
+                       wake_unlock(&client->wake_lock);
        }
 
        if (event->type == EV_SYN && event->code == SYN_REPORT) {
                client->packet_head = client->head;
+               if (client->use_wake_lock)
+                       wake_lock(&client->wake_lock);
                kill_fasync(&client->fasync, SIGIO, POLL_IN);
        }
 }
@@ -289,6 +297,8 @@ static int evdev_release(struct inode *inode, struct file *file)
        mutex_unlock(&evdev->mutex);
 
        evdev_detach_client(evdev, client);
+       if (client->use_wake_lock)
+               wake_lock_destroy(&client->wake_lock);
        kfree(client);
 
        evdev_close_device(evdev);
@@ -320,6 +330,8 @@ static int evdev_open(struct inode *inode, struct file *file)
 
        client->bufsize = bufsize;
        spin_lock_init(&client->buffer_lock);
+       snprintf(client->name, sizeof(client->name), "%s-%d",
+                       dev_name(&evdev->dev), task_tgid_vnr(current));
        client->evdev = evdev;
        evdev_attach_client(evdev, client);
 
@@ -386,6 +398,9 @@ static int evdev_fetch_next_event(struct evdev_client *client,
        if (have_event) {
                *event = client->buffer[client->tail++];
                client->tail &= client->bufsize - 1;
+               if (client->use_wake_lock &&
+                   client->packet_head == client->tail)
+                       wake_unlock(&client->wake_lock);
        }
 
        spin_unlock_irq(&client->buffer_lock);
@@ -674,6 +689,35 @@ static int evdev_handle_mt_request(struct input_dev *dev,
        return 0;
 }
 
+static int evdev_enable_suspend_block(struct evdev *evdev,
+                                     struct evdev_client *client)
+{
+       if (client->use_wake_lock)
+               return 0;
+
+       spin_lock_irq(&client->buffer_lock);
+       wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name);
+       client->use_wake_lock = true;
+       if (client->packet_head != client->tail)
+               wake_lock(&client->wake_lock);
+       spin_unlock_irq(&client->buffer_lock);
+       return 0;
+}
+
+static int evdev_disable_suspend_block(struct evdev *evdev,
+                                      struct evdev_client *client)
+{
+       if (!client->use_wake_lock)
+               return 0;
+
+       spin_lock_irq(&client->buffer_lock);
+       client->use_wake_lock = false;
+       wake_lock_destroy(&client->wake_lock);
+       spin_unlock_irq(&client->buffer_lock);
+
+       return 0;
+}
+
 static long evdev_do_ioctl(struct file *file, unsigned int cmd,
                           void __user *p, int compat_mode)
 {
@@ -755,6 +799,15 @@ static long evdev_do_ioctl(struct file *file, unsigned int cmd,
 
        case EVIOCSKEYCODE_V2:
                return evdev_handle_set_keycode_v2(dev, p);
+
+       case EVIOCGSUSPENDBLOCK:
+               return put_user(client->use_wake_lock, ip);
+
+       case EVIOCSSUSPENDBLOCK:
+               if (p)
+                       return evdev_enable_suspend_block(evdev, client);
+               else
+                       return evdev_disable_suspend_block(evdev, client);
        }
 
        size = _IOC_SIZE(cmd);
index 4649ee35b6052e7ef967b6323003344879774826..93a956489e8786612639248f69718da4741b1e5b 100644 (file)
@@ -153,6 +153,9 @@ struct input_keymap_entry {
 
 #define EVIOCGRAB              _IOW('E', 0x90, int)                    /* Grab/Release device */
 
+#define EVIOCGSUSPENDBLOCK     _IOR('E', 0x91, int)                    /* get suspend block enable */
+#define EVIOCSSUSPENDBLOCK     _IOW('E', 0x91, int)                    /* set suspend block enable */
+
 #define EVIOCSCLOCKID          _IOW('E', 0xa0, int)                    /* Set clockid to be used for timestamps */
 
 /*