HiKey/HiKey960: Rework HiKey PowerHAL to use power@1.1 interface am: c1875b5a33
am: 20f33a65be

Change-Id: Ia9c017fb7e34c481172e5412c1e1b4e460cc6b37
diff --git a/device-common.mk b/device-common.mk
index 40cb0e3..12d3d0e 100644
--- a/device-common.mk
+++ b/device-common.mk
@@ -95,7 +95,9 @@
 endif
 
 # PowerHAL
-PRODUCT_PACKAGES += android.hardware.power@1.0-impl
+PRODUCT_PACKAGES += \
+	android.hardware.power@1.1-impl \
+	android.hardware.power@1.1-service.hikey-common
 
 #GNSS HAL
 PRODUCT_PACKAGES += \
@@ -228,7 +230,6 @@
     android.hardware.graphics.allocator@2.0.vndk-sp\
     android.hardware.graphics.mapper@2.0.vndk-sp\
     android.hardware.graphics.common@1.0.vndk-sp\
-    android.hardware.power@1.0.vndk-sp\
     libvndksupport.vndk-sp\
     libbinder.vndk-sp\
     libhwbinder.vndk-sp\
diff --git a/manifest.xml b/manifest.xml
index 4c5d342..84d82e9 100644
--- a/manifest.xml
+++ b/manifest.xml
@@ -122,8 +122,8 @@
     </hal>
     <hal format="hidl">
         <name>android.hardware.power</name>
-        <transport arch="32+64">passthrough</transport>
-        <version>1.0</version>
+	<transport>hwbinder</transport>
+        <version>1.1</version>
         <interface>
             <name>IPower</name>
             <instance>default</instance>
diff --git a/power/Android.mk b/power/Android.mk
index 53e464e..ca184db 100644
--- a/power/Android.mk
+++ b/power/Android.mk
@@ -19,13 +19,28 @@
 # hw/<POWERS_HARDWARE_MODULE_ID>.<ro.hardware>.so
 include $(CLEAR_VARS)
 
-LOCAL_SHARED_LIBRARIES := liblog libcutils
-LOCAL_SRC_FILES := power_hikey.c
-
 LOCAL_MODULE_RELATIVE_PATH := hw
 LOCAL_VENDOR_MODULE := true
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_MODULE := android.hardware.power@1.1-service.hikey-common
+LOCAL_INIT_RC := android.hardware.power@1.1-service.hikey-common.rc
+LOCAL_SRC_FILES := service.cpp Power.cpp power_hikey.c
+
+#LOCAL_MODULE := power.$(TARGET_BOARD_PLATFORM)
+#LOCAL_SRC_FILES := power_hikey.c
 
 LOCAL_HEADER_LIBRARIES += libhardware_headers
-LOCAL_MODULE := power.$(TARGET_BOARD_PLATFORM)
-LOCAL_MODULE_TAGS := optional
-include $(BUILD_SHARED_LIBRARY)
+
+LOCAL_SHARED_LIBRARIES := liblog libcutils
+
+LOCAL_SHARED_LIBRARIES := \
+    libbase \
+    libcutils \
+    libhidlbase \
+    libhidltransport \
+    liblog \
+    libutils \
+    android.hardware.power@1.1 \
+
+include $(BUILD_EXECUTABLE)
diff --git a/power/Power.cpp b/power/Power.cpp
new file mode 100644
index 0000000..28f096a
--- /dev/null
+++ b/power/Power.cpp
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2017 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 "android.hardware.power@1.1-service.hikey-common"
+
+#include <android/log.h>
+#include <utils/Log.h>
+
+#include <android-base/properties.h>
+
+#include "Power.h"
+#include "power-helper.h"
+
+enum subsystem_type {
+    //Don't add any lines after that line
+    SUBSYSTEM_COUNT
+};
+
+
+namespace android {
+namespace hardware {
+namespace power {
+namespace V1_1 {
+namespace implementation {
+
+using ::android::hardware::power::V1_0::Feature;
+using ::android::hardware::power::V1_0::PowerHint;
+using ::android::hardware::power::V1_0::PowerStatePlatformSleepState;
+using ::android::hardware::power::V1_0::Status;
+using ::android::hardware::power::V1_1::PowerStateSubsystem;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+
+Power::Power() {
+    power_init();
+}
+
+// Methods from ::android::hardware::power::V1_0::IPower follow.
+Return<void> Power::setInteractive(bool interactive)  {
+    power_set_interactive(interactive ? 1 : 0);
+    return Void();
+}
+
+Return<void> Power::powerHint(PowerHint hint, int32_t data) {
+    power_hint(static_cast<power_hint_t>(hint), data ? (&data) : NULL);
+    return Void();
+}
+
+Return<void> Power::setFeature(Feature /*feature*/, bool /*activate*/)  {
+    return Void();
+}
+
+Return<void> Power::getPlatformLowPowerStats(getPlatformLowPowerStats_cb _hidl_cb) {
+
+    hidl_vec<PowerStatePlatformSleepState> states;
+
+    _hidl_cb(states, Status::SUCCESS);
+    return Void();
+}
+
+
+Return<void> Power::getSubsystemLowPowerStats(getSubsystemLowPowerStats_cb _hidl_cb) {
+
+    hidl_vec<PowerStateSubsystem> subsystems;
+    subsystems.resize(subsystem_type::SUBSYSTEM_COUNT);
+
+    //Add query for other subsystems here
+
+    _hidl_cb(subsystems, Status::SUCCESS);
+    return Void();
+}
+
+Return<void> Power::powerHintAsync(PowerHint hint, int32_t data) {
+    // just call the normal power hint in this oneway function
+    return powerHint(hint, data);
+    return Void();
+}
+
+}  // namespace implementation
+}  // namespace V1_1
+}  // namespace power
+}  // namespace hardware
+}  // namespace android
diff --git a/power/Power.h b/power/Power.h
new file mode 100644
index 0000000..2676b7d
--- /dev/null
+++ b/power/Power.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#ifndef ANDROID_HARDWARE_POWER_V1_1_POWER_H
+#define ANDROID_HARDWARE_POWER_V1_1_POWER_H
+
+#include <android/hardware/power/1.1/IPower.h>
+#include <hidl/MQDescriptor.h>
+#include <hidl/Status.h>
+#include <hardware/power.h>
+
+namespace android {
+namespace hardware {
+namespace power {
+namespace V1_1 {
+namespace implementation {
+
+using ::android::hardware::power::V1_0::Feature;
+using ::android::hardware::power::V1_0::PowerHint;
+using ::android::hardware::power::V1_1::IPower;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+
+struct Power : public IPower {
+    // Methods from ::android::hardware::power::V1_0::IPower follow.
+
+    Power();
+
+    Return<void> setInteractive(bool interactive) override;
+    Return<void> powerHint(PowerHint hint, int32_t data) override;
+    Return<void> setFeature(Feature feature, bool activate) override;
+    Return<void> getPlatformLowPowerStats(getPlatformLowPowerStats_cb _hidl_cb) override;
+
+    // Methods from ::android::hardware::power::V1_1::IPower follow.
+    Return<void> getSubsystemLowPowerStats(getSubsystemLowPowerStats_cb _hidl_cb) override;
+    Return<void> powerHintAsync(PowerHint hint, int32_t data) override;
+
+    // Methods from ::android::hidl::base::V1_0::IBase follow.
+
+};
+
+}  // namespace implementation
+}  // namespace V1_1
+}  // namespace power
+}  // namespace hardware
+}  // namespace android
+
+#endif  // ANDROID_HARDWARE_POWER_V1_1_POWER_H
diff --git a/power/android.hardware.power@1.1-service.hikey-common.rc b/power/android.hardware.power@1.1-service.hikey-common.rc
new file mode 100644
index 0000000..4a1e1e1
--- /dev/null
+++ b/power/android.hardware.power@1.1-service.hikey-common.rc
@@ -0,0 +1,4 @@
+service vendor.power-hal-1-1 /vendor/bin/hw/android.hardware.power@1.1-service.hikey-common
+    class hal
+    user system
+    group system
diff --git a/power/power-helper.h b/power/power-helper.h
new file mode 100644
index 0000000..1c3ae98
--- /dev/null
+++ b/power/power-helper.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2017, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * *    * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of The Linux Foundation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef __POWER_HELPER_H__
+#define __POWER_HELPER_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <hardware/power.h>
+
+void power_init(void);
+void power_hint(power_hint_t hint, void *data);
+void power_set_interactive(int on);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif //__POWER_HELPER_H__
diff --git a/power/power_hikey.c b/power/power_hikey.c
index f9a3ddb..a2eb373 100644
--- a/power/power_hikey.c
+++ b/power/power_hikey.c
@@ -36,6 +36,8 @@
 #include <hardware/hardware.h>
 #include <hardware/power.h>
 
+#include "power-helper.h"
+
 #define SCHEDTUNE_BOOST_PATH "/dev/stune/top-app/schedtune.boost"
 #define SCHEDTUNE_BOOST_VAL_PROP "ro.config.schetune.touchboost.value"
 #define SCHEDTUNE_BOOST_TIME_PROP "ro.config.schetune.touchboost.time_ns"
@@ -79,6 +81,8 @@
     sem_t signal_lock;
 };
 
+struct hikey_power_module this_power_module;
+
 
 static bool low_power_mode = false;
 
@@ -248,7 +252,7 @@
 
 /*[schedtune functions]*******************************************************/
 
-int schedtune_sysfs_boost(struct hikey_power_module *hikey, char* booststr)
+static int schedtune_sysfs_boost(struct hikey_power_module *hikey, char* booststr)
 {
     char buf[80];
     int len;
@@ -348,7 +352,7 @@
 
 /*[generic functions]*********************************************************/
 
-static void hikey_cpufreq_set_interactive(struct power_module __unused *module, int on)
+void power_set_interactive(int on)
 {
     int i;
 
@@ -396,10 +400,14 @@
     max_clusters = i;
 }
 
-static void hikey_power_init(struct power_module __unused *module)
+void power_init(void)
 {
-    struct hikey_power_module *hikey = container_of(module,
-                                              struct hikey_power_module, base);
+    struct hikey_power_module *hikey = &this_power_module;
+    memset(hikey, 0, sizeof(struct hikey_power_module));
+    pthread_mutex_init(&hikey->lock, NULL);
+    hikey->boostpulse_fd = -1;
+    hikey->boostpulse_warned = 0;
+
     hikey_cpufreq_init(hikey);
     hikey_devfreq_init(hikey);
     interactive_power_init(hikey);
@@ -416,11 +424,9 @@
         return;
 }
 
-static void hikey_power_hint(struct power_module *module, power_hint_t hint,
-                                void *data)
+void power_hint(power_hint_t hint, void *data)
 {
-    struct hikey_power_module *hikey = container_of(module,
-                                              struct hikey_power_module, base);
+    struct hikey_power_module *hikey = &this_power_module;
 
     pthread_mutex_lock(&hikey->lock);
     switch (hint) {
@@ -433,7 +439,7 @@
 
     case POWER_HINT_LOW_POWER:
         low_power_mode = data;
-        hikey_cpufreq_set_interactive(module, 1);
+        power_set_interactive(1);
         break;
 
     default:
@@ -441,68 +447,3 @@
     }
     pthread_mutex_unlock(&hikey->lock);
 }
-
-static void set_feature(struct power_module __unused *module,
-                        feature_t feature, int state)
-{
-    switch (feature) {
-    default:
-        ALOGW("Error setting the feature %d and state %d, it doesn't exist\n",
-              feature, state);
-        break;
-    }
-}
-
-static int power_open(const hw_module_t* __unused module, const char* name,
-                    hw_device_t** device)
-{
-    int retval = 0; /* 0 is ok; -1 is error */
-    ALOGD("%s: enter; name=%s", __FUNCTION__, name);
-
-    if (strcmp(name, POWER_HARDWARE_MODULE_ID) == 0) {
-        struct hikey_power_module *dev = (struct hikey_power_module *)calloc(1,
-                sizeof(struct hikey_power_module));
-
-        if (dev) {
-            /* Common hw_device_t fields */
-            dev->base.common.tag = HARDWARE_DEVICE_TAG;
-            dev->base.common.module_api_version = POWER_MODULE_API_VERSION_0_5;
-            dev->base.common.hal_api_version = HARDWARE_HAL_API_VERSION;
-
-            dev->base.init = hikey_power_init;
-            dev->base.powerHint = hikey_power_hint;
-            dev->base.setInteractive = hikey_cpufreq_set_interactive;
-            dev->base.setFeature = set_feature;
-
-            pthread_mutex_init(&dev->lock, NULL);
-            dev->boostpulse_fd = -1;
-            dev->boostpulse_warned = 0;
-
-            *device = (hw_device_t*)&dev->base;
-        } else
-            retval = -ENOMEM;
-    } else {
-        retval = -EINVAL;
-    }
-
-    ALOGD("%s: exit %d", __FUNCTION__, retval);
-    return retval;
-}
-
-static struct hw_module_methods_t power_module_methods = {
-    .open = power_open,
-};
-
-struct hikey_power_module HAL_MODULE_INFO_SYM = {
-    .base = {
-        .common = {
-            .tag = HARDWARE_MODULE_TAG,
-            .module_api_version = POWER_MODULE_API_VERSION_0_2,
-            .hal_api_version = HARDWARE_HAL_API_VERSION,
-            .id = POWER_HARDWARE_MODULE_ID,
-            .name = "HiKey Power HAL",
-            .author = "The Android Open Source Project",
-            .methods = &power_module_methods,
-        },
-    },
-};
diff --git a/power/service.cpp b/power/service.cpp
new file mode 100644
index 0000000..3cb0b68
--- /dev/null
+++ b/power/service.cpp
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2017 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 "android.hardware.power@1.1-service.hikey-common"
+
+#include <android/log.h>
+#include <hidl/HidlTransportSupport.h>
+#include <hardware/power.h>
+#include "Power.h"
+
+using android::sp;
+using android::status_t;
+using android::OK;
+
+// libhwbinder:
+using android::hardware::configureRpcThreadpool;
+using android::hardware::joinRpcThreadpool;
+
+// Generated HIDL files
+using android::hardware::power::V1_1::IPower;
+using android::hardware::power::V1_1::implementation::Power;
+
+int main() {
+
+    status_t status;
+    android::sp<IPower> service = nullptr;
+
+    ALOGI("Power HAL Service 1.1 for HiKey-common is starting.");
+
+    service = new Power();
+    if (service == nullptr) {
+        ALOGE("Can not create an instance of Power HAL Iface, exiting.");
+
+        goto shutdown;
+    }
+
+    configureRpcThreadpool(1, true /*callerWillJoin*/);
+
+    status = service->registerAsService();
+    if (status != OK) {
+        ALOGE("Could not register service for Power HAL Iface (%d).", status);
+        goto shutdown;
+    }
+
+    ALOGI("Power Service is ready");
+    joinRpcThreadpool();
+    //Should not pass this line
+
+shutdown:
+    // In normal operation, we don't expect the thread pool to exit
+
+    ALOGE("Power Service is shutting down");
+    return 1;
+}
diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts
index dacdb9f..52a29e5 100644
--- a/sepolicy/file_contexts
+++ b/sepolicy/file_contexts
@@ -34,6 +34,8 @@
 /dev/block/platform/soc/f723d000\.dwmmc0/by-name/userdata u:object_r:userdata_block_device:s0
 
 
+/vendor/bin/hw/android\.hardware\.power@1\.1-service\.hikey-common          u:object_r:hal_power_default_exec:s0
+
 /vendor/lib(64)?/libRSDriverArm\.so u:object_r:same_process_hal_file:s0
 /vendor/lib64/libbccArm\.so u:object_r:same_process_hal_file:s0
 /vendor/lib64/libbcc\.so u:object_r:same_process_hal_file:s0
diff --git a/sepolicy/hal_power.te b/sepolicy/hal_power.te
new file mode 100644
index 0000000..476a603
--- /dev/null
+++ b/sepolicy/hal_power.te
@@ -0,0 +1,9 @@
+typeattribute hal_power_default data_between_core_and_vendor_violators;
+
+allow hal_power_default cgroup:dir search;
+allow hal_power_default cgroup:file rw_file_perms;
+
+allow hal_power_default sysfs_devices_system_cpu:file rw_file_perms;
+allow hal_power_default sysfs_power:file rw_file_perms;
+
+