Move EventHub over to using EPOLLWAKEUP when available

Bug: 10901016
Change-Id: If63c0de5cf120cdf1534d63fc2382d2925b35c6a
diff --git a/core/java/com/android/internal/os/ZygoteInit.java b/core/java/com/android/internal/os/ZygoteInit.java
index 04351da..afc5361 100644
--- a/core/java/com/android/internal/os/ZygoteInit.java
+++ b/core/java/com/android/internal/os/ZygoteInit.java
@@ -484,6 +484,7 @@
     private static boolean startSystemServer()
             throws MethodAndArgsCaller, RuntimeException {
         long capabilities = posixCapabilitiesAsBits(
+            OsConstants.CAP_BLOCK_SUSPEND,
             OsConstants.CAP_KILL,
             OsConstants.CAP_NET_ADMIN,
             OsConstants.CAP_NET_BIND_SERVICE,
diff --git a/services/input/EventHub.cpp b/services/input/EventHub.cpp
index 4d70d5f..cd313c4 100644
--- a/services/input/EventHub.cpp
+++ b/services/input/EventHub.cpp
@@ -49,6 +49,7 @@
 #include <sys/ioctl.h>
 #include <sys/limits.h>
 #include <sys/sha1.h>
+#include <sys/utsname.h>
 
 /* this macro is used to tell if "bit" is set in "array"
  * it selects a byte from the array, and does a boolean AND
@@ -93,6 +94,14 @@
     return out;
 }
 
+static void getLinuxRelease(int* major, int* minor) {
+    struct utsname info;
+    if (uname(&info) || sscanf(info.release, "%d.%d", major, minor) <= 0) {
+        *major = 0, *minor = 0;
+        ALOGE("Could not get linux version: %s", strerror(errno));
+    }
+}
+
 static void setDescriptor(InputDeviceIdentifier& identifier) {
     // Compute a device descriptor that uniquely identifies the device.
     // The descriptor is assumed to be a stable identifier.  Its value should not
@@ -236,6 +245,11 @@
     result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &eventItem);
     LOG_ALWAYS_FATAL_IF(result != 0, "Could not add wake read pipe to epoll instance.  errno=%d",
             errno);
+
+    int major, minor;
+    getLinuxRelease(&major, &minor);
+    // EPOLLWAKEUP was introduced in kernel 3.5
+    mUsingEpollWakeup = major > 3 || (major == 3 && minor >= 5);
 }
 
 EventHub::~EventHub(void) {
@@ -1244,7 +1258,7 @@
     // Register with epoll.
     struct epoll_event eventItem;
     memset(&eventItem, 0, sizeof(eventItem));
-    eventItem.events = EPOLLIN;
+    eventItem.events = mUsingEpollWakeup ? EPOLLIN : EPOLLIN | EPOLLWAKEUP;
     eventItem.data.u32 = deviceId;
     if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, &eventItem)) {
         ALOGE("Could not add device fd to epoll instance.  errno=%d", errno);
@@ -1252,9 +1266,14 @@
         return -1;
     }
 
-    // Enable wake-lock behavior on kernels that support it.
-    // TODO: Only need this for devices that can really wake the system.
-    bool usingSuspendBlockIoctl = !ioctl(fd, EVIOCSSUSPENDBLOCK, 1);
+    String8 wakeMechanism("EPOLLWAKEUP");
+    if (!mUsingEpollWakeup) {
+        if (ioctl(fd, EVIOCSSUSPENDBLOCK, 1)) {
+            wakeMechanism = "<none>";
+        } else {
+            wakeMechanism = "EVIOCSSUSPENDBLOCK";
+        }
+    }
 
     // Tell the kernel that we want to use the monotonic clock for reporting timestamps
     // associated with input events.  This is important because the input system
@@ -1276,14 +1295,14 @@
 
     ALOGI("New device: id=%d, fd=%d, path='%s', name='%s', classes=0x%x, "
             "configuration='%s', keyLayout='%s', keyCharacterMap='%s', builtinKeyboard=%s, "
-            "usingSuspendBlockIoctl=%s, usingClockIoctl=%s",
+            "wakeMechanism=%s, usingClockIoctl=%s",
          deviceId, fd, devicePath, device->identifier.name.string(),
          device->classes,
          device->configurationFile.string(),
          device->keyMap.keyLayoutFile.string(),
          device->keyMap.keyCharacterMapFile.string(),
          toString(mBuiltInKeyboardId == deviceId),
-         toString(usingSuspendBlockIoctl), toString(usingClockIoctl));
+         wakeMechanism.string(), toString(usingClockIoctl));
 
     addDeviceLocked(device);
     return 0;
diff --git a/services/input/EventHub.h b/services/input/EventHub.h
index ae28f01..e54b9fc 100644
--- a/services/input/EventHub.h
+++ b/services/input/EventHub.h
@@ -440,6 +440,8 @@
     size_t mPendingEventCount;
     size_t mPendingEventIndex;
     bool mPendingINotify;
+
+    bool mUsingEpollWakeup;
 };
 
 }; // namespace android
diff --git a/services/input/InputReader.h b/services/input/InputReader.h
index a8bb636..e6f45b6 100644
--- a/services/input/InputReader.h
+++ b/services/input/InputReader.h
@@ -574,8 +574,8 @@
 private:
     InputReaderContext* mContext;
     int32_t mId;
-    int32_t mControllerNumber;
     int32_t mGeneration;
+    int32_t mControllerNumber;
     InputDeviceIdentifier mIdentifier;
     String8 mAlias;
     uint32_t mClasses;