Merge "Angler: NFC: Enable HCE-Felica support." into nyc-dev
diff --git a/device.mk b/device.mk
index a5dd06e..12b5be9 100644
--- a/device.mk
+++ b/device.mk
@@ -247,7 +247,8 @@
 
 # Power HAL
 PRODUCT_PACKAGES += \
-    power.angler
+    power.angler \
+    hardware_properties.angler
 
 PRODUCT_COPY_FILES += \
     device/huawei/angler/nfc/libnfc-brcm.conf:system/etc/libnfc-brcm.conf \
@@ -285,6 +286,10 @@
     ro.hwui.text_large_cache_width=2048 \
     ro.hwui.text_large_cache_height=1024
 
+# VR HAL
+PRODUCT_PACKAGES += \
+    vr.angler
+
 # Enable low power video mode for 4K encode
 PRODUCT_PROPERTY_OVERRIDES += \
     vidc.debug.perf.mode=2 \
diff --git a/hardware_properties/Android.mk b/hardware_properties/Android.mk
new file mode 100644
index 0000000..fd9e55e
--- /dev/null
+++ b/hardware_properties/Android.mk
@@ -0,0 +1,26 @@
+#
+# Copyright 2016 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_CFLAGS := -Wno-unused-parameter
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_SRC_FILES := hardware_properties.c
+LOCAL_SHARED_LIBRARIES := liblog libcutils
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE := hardware_properties.angler
+include $(BUILD_SHARED_LIBRARY)
diff --git a/hardware_properties/hardware_properties.c b/hardware_properties/hardware_properties.c
new file mode 100644
index 0000000..0f37344
--- /dev/null
+++ b/hardware_properties/hardware_properties.c
@@ -0,0 +1,233 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <ctype.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define LOG_TAG "HardwarePropertiesHAL"
+#include <utils/Log.h>
+
+#include <hardware/hardware.h>
+#include <hardware/hardware_properties.h>
+
+#define MAX_LENGTH              50
+
+#define CPU_USAGE_FILE          "/proc/stat"
+#define TEMPERATURE_FILE_FORMAT "/sys/class/thermal/thermal_zone%d/temp"
+#define BATTERY_TEMP_NUM        23
+#define GPU_TEMP_NUM            13
+
+const int CPU_SENSORS[] = {8, 9, 10, 11, 14, 15, 16, 7};
+
+#define CPU_NUM                 (sizeof(CPU_SENSORS) / sizeof(int))
+
+static ssize_t get_cpu_temperatures(struct hardware_properties_module *module,
+                                    float **temps) {
+    FILE *file;
+    char file_name[MAX_LENGTH];
+    int cpu;
+    float temp;
+    size_t size = 0;
+
+    *temps = malloc(CPU_NUM * sizeof(float));
+
+    for (cpu = 0; cpu < CPU_NUM; cpu++) {
+        sprintf(file_name, TEMPERATURE_FILE_FORMAT, CPU_SENSORS[cpu]);
+        file = fopen(file_name, "r");
+        if (file == NULL) {
+            ALOGE("%s: failed to open: %s", __func__, strerror(errno));
+            free(*temps);
+            *temps = NULL;
+            return -1;
+        }
+        if (1 != fscanf(file, "%f", &temp)) {
+            fclose(file);
+            free(*temps);
+            *temps = NULL;
+            ALOGE("%s: failed to read a float: %s", __func__, strerror(errno));
+            return -1;
+        }
+        // tsens_tz_sensor[7,8,9,10,11,14,15,6]: temperature in Celsius.
+        (*temps)[size] = temp;
+        size++;
+        fclose(file);
+    }
+    return size;
+}
+
+static ssize_t get_battery_temperatures(
+        struct hardware_properties_module *module, float **temps) {
+    FILE *file;
+    char file_name[MAX_LENGTH];
+    float temp;
+
+    *temps = malloc(sizeof(float));
+
+    sprintf(file_name, TEMPERATURE_FILE_FORMAT, BATTERY_TEMP_NUM);
+    file = fopen(file_name, "r");
+
+    if (file == NULL) {
+        ALOGE("%s: failed to open: %s", __func__, strerror(errno));
+        free(*temps);
+        *temps = NULL;
+        return -1;
+    }
+
+    if (1 != fscanf(file, "%f", &temp)) {
+        fclose(file);
+        free(*temps);
+        *temps = NULL;
+        ALOGE("%s: failed to read a float: %s", __func__, strerror(errno));
+        return -1;
+    }
+
+    // hwmon sensor: battery: temperature in millidegrees Celsius.
+    (*temps)[0] = temp / 1000;
+    fclose(file);
+    return 1;
+}
+
+static ssize_t get_gpu_temperatures(struct hardware_properties_module *module,
+                                    float **temps) {
+    FILE *file;
+    char file_name[MAX_LENGTH];
+    float temp;
+
+    *temps = malloc(sizeof(float));
+
+    sprintf(file_name, TEMPERATURE_FILE_FORMAT, GPU_TEMP_NUM);
+    file = fopen(file_name, "r");
+
+    if (file == NULL) {
+        ALOGE("%s: failed to open: %s", __func__, strerror(errno));
+        free(*temps);
+        *temps = NULL;
+        return -1;
+    }
+
+    if (1 != fscanf(file, "%f", &temp)) {
+        fclose(file);
+        free(*temps);
+        *temps = NULL;
+        ALOGE("%s: failed to read a float: %s", __func__, strerror(errno));
+        return -1;
+    }
+
+    // tsens_tz_sensor12: temperature in Celsius.
+    (*temps)[0] = temp;
+    fclose(file);
+    return 1;
+}
+
+static ssize_t get_cpu_usages(struct hardware_properties_module *module,
+                              int64_t **active_times,
+                              int64_t **total_times) {
+    int vals, cpu_num;
+    ssize_t read;
+    uint64_t user, nice, system, idle, active, total;
+    char *line = NULL;
+    size_t len = 0;
+    size_t size = 0;
+    FILE *file = fopen(CPU_USAGE_FILE, "r");
+
+    if (file == NULL) {
+        ALOGE("%s: failed to open: %s", __func__, strerror(errno));
+        return -1;
+    }
+
+    *active_times = malloc(CPU_NUM * sizeof(int64_t));
+    *total_times = malloc(CPU_NUM * sizeof(int64_t));
+
+    while((read = getline(&line, &len, file)) != -1) {
+        // Skip non "cpu[0-9]" lines.
+        if (strnlen(line, read) < 4 || strncmp(line, "cpu", 3) != 0
+            || !isdigit(line[3])) {
+            free(line);
+            line = NULL;
+            len = 0;
+            continue;
+        }
+
+        vals = sscanf(line, "cpu%d %" SCNu64 " %" SCNu64 " %" SCNu64 " %"
+                      SCNu64, &cpu_num, &user, &nice, &system, &idle);
+
+        if (vals != 5 || size == CPU_NUM) {
+            if (vals != 5) {
+                ALOGE("%s: failed to read CPU information from file: %s",
+                      __func__, strerror(errno));
+            } else {
+                ALOGE("/proc/stat file has incorrect format.");
+            }
+            fclose(file);
+            free(line);
+            free(*active_times);
+            free(*total_times);
+            *active_times = NULL;
+            *total_times = NULL;
+            line = NULL;
+            len = 0;
+            return -1;
+        }
+
+        active = user + nice + system;
+        total = active + idle;
+        (*active_times)[size] = active;
+        (*total_times)[size] = total;
+
+        size++;
+
+        free(line);
+        line = NULL;
+        len = 0;
+    }
+
+    if (size != CPU_NUM) {
+        ALOGE("/proc/stat file has incorrect format.");
+        fclose(file);
+        free(*active_times);
+        *active_times = NULL;
+        free(*total_times);
+        *total_times = NULL;
+        return -1;
+    }
+
+    fclose(file);
+    return size;
+}
+
+static struct hw_module_methods_t hardware_properties_module_methods = {
+    .open = NULL,
+};
+
+struct hardware_properties_module HAL_MODULE_INFO_SYM = {
+    .common = {
+        .tag = HARDWARE_MODULE_TAG,
+        .module_api_version
+            = HARDWARE_PROPERTIES_HARDWARE_MODULE_API_VERSION_0_1,
+        .hal_api_version = HARDWARE_HAL_API_VERSION,
+        .id = HARDWARE_PROPERTIES_HARDWARE_MODULE_ID,
+        .name = "Angler Hardware Properties HAL",
+        .author = "The Android Open Source Project",
+        .methods = &hardware_properties_module_methods,
+    },
+    .getCpuTemperatures = get_cpu_temperatures,
+    .getGpuTemperatures = get_gpu_temperatures,
+    .getBatteryTemperatures = get_battery_temperatures,
+    .getCpuUsages = get_cpu_usages,
+};
diff --git a/init.angler.rc b/init.angler.rc
index e593451..988ff88 100755
--- a/init.angler.rc
+++ b/init.angler.rc
@@ -129,6 +129,9 @@
     chown system system /sys/class/leds/blue/on_off_ms
     chown system system /sys/class/leds/red/rgb_start
 
+    # GPU Tuning
+    chmod 0666 /sys/class/kgsl/kgsl-3d0/dispatch/inflight
+
 on property:init.svc.per_mgr=running
     start per_proxy
 
diff --git a/sepolicy/file.te b/sepolicy/file.te
index 95c7939..1691dcb 100644
--- a/sepolicy/file.te
+++ b/sepolicy/file.te
@@ -18,6 +18,7 @@
 type qmuxd_socket, file_type;
 type ssr_data_file, file_type, data_file_type;
 type sysfs_led, sysfs_type, fs_type;
+type sysfs_gpu_tuning, sysfs_type, fs_type;
 type sysfs_fingerprintd, sysfs_type, fs_type;
 type sysfs_msm_subsys, sysfs_type, fs_type;
 type sysfs_msm_subsys_toggle, sysfs_type, fs_type;
diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts
index 4495bff..a32e6c9 100644
--- a/sepolicy/file_contexts
+++ b/sepolicy/file_contexts
@@ -86,6 +86,7 @@
 /sys/devices/soc\.0/qpnp-smbcharger-[^/]*/power_supply/battery/system_temp_level                           u:object_r:sysfs_thermal:s0
 /sys/devices/soc\.0/qpnp-smbcharger-[0-9a-f]+/power_supply/battery/capacity                                u:object_r:sysfs_batteryinfo:s0
 /sys/devices/soc\.0/fdb00000.qcom,kgsl-3d0/kgsl/kgsl-3d0/max_gpuclk                                        u:object_r:sysfs_thermal:s0
+/sys/devices/soc\.0/fdb00000.qcom,kgsl-3d0/kgsl/kgsl-3d0/dispatch/inflight                                 u:object_r:sysfs_gpu_tuning:s0
 
 # Sysfs files used by fingerprintd
 /sys/devices/soc\.0/fpc1020\.[0-9]+(/.*)?                           u:object_r:sysfs_fingerprintd:s0
diff --git a/sepolicy/system_server.te b/sepolicy/system_server.te
index 2b1bd08..bef0510 100644
--- a/sepolicy/system_server.te
+++ b/sepolicy/system_server.te
@@ -5,6 +5,9 @@
 allow system_server sysfs_led:dir r_dir_perms;
 allow system_server sysfs_led:file rw_file_perms;
 
+allow system_server sysfs_gpu_tuning:dir r_dir_perms;
+allow system_server sysfs_gpu_tuning:file rw_file_perms;
+
 allow system_server sysfs_msm_subsys:dir r_dir_perms;
 allow system_server sysfs_msm_subsys:lnk_file r_file_perms;
 
diff --git a/sepolicy/ueventd.te b/sepolicy/ueventd.te
index 185483a..31109e9 100644
--- a/sepolicy/ueventd.te
+++ b/sepolicy/ueventd.te
@@ -1,6 +1,7 @@
 allow ueventd firmware_file:dir r_dir_perms;
 allow ueventd firmware_file:file r_file_perms;
 allow ueventd sysfs_led:file rw_file_perms;
+allow ueventd sysfs_gpu_tuning:file rw_file_perms;
 allow ueventd sysfs_thermal:file rw_file_perms;
 #read and write /sys/bus/spi/devices/spi12.0/spi_prepare
 allow ueventd sysfs_fingerprintd:file rw_file_perms;
diff --git a/vr/Android.mk b/vr/Android.mk
new file mode 100644
index 0000000..b35f550
--- /dev/null
+++ b/vr/Android.mk
@@ -0,0 +1,26 @@
+# Copyright 2016 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := vr.c
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_SHARED_LIBRARIES := liblog libcutils
+LOCAL_CFLAGS += -Wno-unused-parameter
+LOCAL_MODULE := vr.angler
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/vr/vr.c b/vr/vr.c
new file mode 100644
index 0000000..0316a55
--- /dev/null
+++ b/vr/vr.c
@@ -0,0 +1,143 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "VrHALImpl"
+
+#include <cutils/log.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <hardware/vr.h>
+#include <hardware/hardware.h>
+
+
+static const int DEFAULT_GPU_INFLIGHT = 15;
+static const int VR_MODE_GPU_INFLIGHT = 2;
+static const char* GPU_INFLIGHT_PATH = "/sys/class/kgsl/kgsl-3d0/dispatch/inflight";
+
+/**
+ * Write 'len' characters from 'input' character array into file at path 'outFile.'
+ *
+ * Return 0 on success, or a negative error code.
+ */
+static int write_string(const char* input, size_t len, const char* outFile) {
+    int fd = -1;
+    ssize_t err = 0;
+
+    // Check input strings.
+    if (input == NULL || outFile == NULL) {
+        ALOGE("%s: Invalid input to write", __FUNCTION__);
+        return -1;
+    }
+
+    // Open file, check for errors.
+    fd = open(outFile, O_WRONLY);
+    if (fd < 0) {
+        ALOGE("%s: Failed to open file %s, error %s (%d)", __FUNCTION__, outFile, strerror(errno),
+              -errno);
+        return -errno;
+    }
+
+    // Write file, check for errors.
+    err = write(fd, input, len);
+    if (err < 0) {
+        ALOGE("%s: Failed to write file %s, error %s (%d)", __FUNCTION__, outFile, strerror(errno),
+              -errno);
+        close(fd);
+        return -errno;
+    }
+
+    // Close and return success.
+    close(fd);
+    return 0;
+}
+
+/**
+ * Write integer 'input' formatted as a character string into the file at path 'outFile.'
+ *
+ * Return 0 on success, or a negative error code.
+ */
+static int write_int(int input, const char* outFile) {
+    char buffer[128] = {0,};
+    int bytes = snprintf(buffer, sizeof(buffer), "%d", input);
+
+    if (bytes < 0 || (size_t) bytes >= sizeof(buffer)) {
+        ALOGE("%s: Failed to format integer %d", __FUNCTION__, input);
+        return -EINVAL;
+    }
+
+    return write_string(buffer, (size_t) bytes, outFile);
+}
+
+// Set global display/GPU/scheduler configuration to used for VR apps.
+static void set_vr_performance_configuration() {
+    int err = 0;
+
+    // Set in-flight buffers to 2.
+    err = write_int(VR_MODE_GPU_INFLIGHT, GPU_INFLIGHT_PATH);
+
+    if (err < 0) {
+        ALOGW("%s: Error while setting configuration for VR mode.", __FUNCTION__);
+    }
+}
+
+// Reset to default global display/GPU/scheduler configuration.
+static void unset_vr_performance_configuration() {
+    int err = 0;
+
+    // Set in-flight buffers back to default (15).
+    err = write_int(DEFAULT_GPU_INFLIGHT, GPU_INFLIGHT_PATH);
+
+    if (err < 0) {
+        ALOGW("%s: Error while setting configuration for VR mode.", __FUNCTION__);
+    }
+}
+
+static void vr_init(struct vr_module *module) {
+    // NOOP
+}
+
+static void vr_set_vr_mode(struct vr_module *module, bool enabled) {
+    if (enabled) {
+        set_vr_performance_configuration();
+    } else {
+        unset_vr_performance_configuration();
+    }
+}
+
+static struct hw_module_methods_t vr_module_methods = {
+    .open = NULL, // There are no devices for this HAL interface.
+};
+
+
+vr_module_t HAL_MODULE_INFO_SYM = {
+    .common = {
+        .tag                = HARDWARE_MODULE_TAG,
+        .module_api_version = VR_MODULE_API_VERSION_1_0,
+        .hal_api_version    = HARDWARE_HAL_API_VERSION,
+        .id                 = VR_HARDWARE_MODULE_ID,
+        .name               = "Angler VR HAL",
+        .author             = "The Android Open Source Project",
+        .methods            = &vr_module_methods,
+    },
+
+    .init = vr_init,
+    .set_vr_mode = vr_set_vr_mode,
+};