From c604e3fb9fd528c56fdcf267477f757f03830a14 Mon Sep 17 00:00:00 2001 From: Jan Altensen Date: Sun, 3 Apr 2022 03:19:52 +0900 Subject: [PATCH] universal7885: hidl: Uprev usb HAl to 1.3 * From LineageOS hardware/samsung --- .gitignore | 5 - a20/device.mk | 2 +- a20e/device.mk | 2 +- a30/device.mk | 2 +- a40/device.mk | 2 +- .../samsung/{ => hidl}/usb/typeb/Android.bp | 0 .../samsung/{ => hidl}/usb/typeb/Usb.cpp | 0 .../samsung/{ => hidl}/usb/typeb/Usb.h | 0 .../android.hardware.usb@1.0-service.a10.rc | 0 .../android.hardware.usb@1.0-service.a10.xml | 0 .../samsung/{ => hidl}/usb/typeb/service.cpp | 0 .../samsung/{ => hidl}/usb/typec/Android.bp | 18 +- .../samsung/{ => hidl}/usb/typec/Usb.cpp | 443 ++++++++++-------- .../samsung/{ => hidl}/usb/typec/Usb.h | 57 ++- ...ndroid.hardware.usb@1.3-service.samsung.rc | 16 + ...droid.hardware.usb@1.3-service.samsung.xml | 12 + .../samsung/{ => hidl}/usb/typec/service.cpp | 9 +- ...oid.hardware.usb@1.1-service.exynos7885.rc | 14 - .../sepolicy/vendor/file_contexts | 2 +- 19 files changed, 338 insertions(+), 246 deletions(-) delete mode 100644 .gitignore rename universal7885-common/hardware/samsung/{ => hidl}/usb/typeb/Android.bp (100%) mode change 100755 => 100644 rename universal7885-common/hardware/samsung/{ => hidl}/usb/typeb/Usb.cpp (100%) rename universal7885-common/hardware/samsung/{ => hidl}/usb/typeb/Usb.h (100%) rename universal7885-common/hardware/samsung/{ => hidl}/usb/typeb/android.hardware.usb@1.0-service.a10.rc (100%) mode change 100755 => 100644 rename universal7885-common/hardware/samsung/{ => hidl}/usb/typeb/android.hardware.usb@1.0-service.a10.xml (100%) rename universal7885-common/hardware/samsung/{ => hidl}/usb/typeb/service.cpp (100%) mode change 100755 => 100644 rename universal7885-common/hardware/samsung/{ => hidl}/usb/typec/Android.bp (70%) rename universal7885-common/hardware/samsung/{ => hidl}/usb/typec/Usb.cpp (59%) rename universal7885-common/hardware/samsung/{ => hidl}/usb/typec/Usb.h (53%) create mode 100644 universal7885-common/hardware/samsung/hidl/usb/typec/android.hardware.usb@1.3-service.samsung.rc create mode 100644 universal7885-common/hardware/samsung/hidl/usb/typec/android.hardware.usb@1.3-service.samsung.xml rename universal7885-common/hardware/samsung/{ => hidl}/usb/typec/service.cpp (83%) delete mode 100644 universal7885-common/hardware/samsung/usb/typec/android.hardware.usb@1.1-service.exynos7885.rc diff --git a/.gitignore b/.gitignore deleted file mode 100644 index f7f2322..0000000 --- a/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ - -universal7885-common/vendor_name -a20e/AndroidProducts.mk -a20/AndroidProducts.mk - diff --git a/a20/device.mk b/a20/device.mk index b90deb7..984b599 100644 --- a/a20/device.mk +++ b/a20/device.mk @@ -12,7 +12,7 @@ DERP_BUILDTYPE := Official # USB PRODUCT_PACKAGES += \ - android.hardware.usb@1.1-service.exynos7885 + android.hardware.usb@1.3-service.samsung TARGET_SCREEN_HEIGHT := 1560 TARGET_SCREEN_WIDTH := 720 diff --git a/a20e/device.mk b/a20e/device.mk index 3adf755..63c5854 100644 --- a/a20e/device.mk +++ b/a20e/device.mk @@ -12,7 +12,7 @@ DERP_BUILDTYPE := Official # USB PRODUCT_PACKAGES += \ - android.hardware.usb@1.1-service.exynos7885 + android.hardware.usb@1.3-service.samsung TARGET_SCREEN_HEIGHT := 1560 TARGET_SCREEN_WIDTH := 720 diff --git a/a30/device.mk b/a30/device.mk index baab9a9..fd19e16 100644 --- a/a30/device.mk +++ b/a30/device.mk @@ -12,7 +12,7 @@ TARGET_SCREEN_WIDTH := 1080 # USB PRODUCT_PACKAGES += \ - android.hardware.usb@1.1-service.exynos7885 + android.hardware.usb@1.3-service.samsung # Feature Permissions PRODUCT_COPY_FILES += \ diff --git a/a40/device.mk b/a40/device.mk index cd2216b..07a166e 100644 --- a/a40/device.mk +++ b/a40/device.mk @@ -9,7 +9,7 @@ $(call inherit-product, frameworks/native/build/phone-xhdpi-4096-dalvik-heap.mk) # USB PRODUCT_PACKAGES += \ - android.hardware.usb@1.1-service.exynos7885 + android.hardware.usb@1.3-service.samsung TARGET_SCREEN_HEIGHT := 2280 TARGET_SCREEN_WIDTH := 1080 diff --git a/universal7885-common/hardware/samsung/usb/typeb/Android.bp b/universal7885-common/hardware/samsung/hidl/usb/typeb/Android.bp old mode 100755 new mode 100644 similarity index 100% rename from universal7885-common/hardware/samsung/usb/typeb/Android.bp rename to universal7885-common/hardware/samsung/hidl/usb/typeb/Android.bp diff --git a/universal7885-common/hardware/samsung/usb/typeb/Usb.cpp b/universal7885-common/hardware/samsung/hidl/usb/typeb/Usb.cpp similarity index 100% rename from universal7885-common/hardware/samsung/usb/typeb/Usb.cpp rename to universal7885-common/hardware/samsung/hidl/usb/typeb/Usb.cpp diff --git a/universal7885-common/hardware/samsung/usb/typeb/Usb.h b/universal7885-common/hardware/samsung/hidl/usb/typeb/Usb.h similarity index 100% rename from universal7885-common/hardware/samsung/usb/typeb/Usb.h rename to universal7885-common/hardware/samsung/hidl/usb/typeb/Usb.h diff --git a/universal7885-common/hardware/samsung/usb/typeb/android.hardware.usb@1.0-service.a10.rc b/universal7885-common/hardware/samsung/hidl/usb/typeb/android.hardware.usb@1.0-service.a10.rc old mode 100755 new mode 100644 similarity index 100% rename from universal7885-common/hardware/samsung/usb/typeb/android.hardware.usb@1.0-service.a10.rc rename to universal7885-common/hardware/samsung/hidl/usb/typeb/android.hardware.usb@1.0-service.a10.rc diff --git a/universal7885-common/hardware/samsung/usb/typeb/android.hardware.usb@1.0-service.a10.xml b/universal7885-common/hardware/samsung/hidl/usb/typeb/android.hardware.usb@1.0-service.a10.xml similarity index 100% rename from universal7885-common/hardware/samsung/usb/typeb/android.hardware.usb@1.0-service.a10.xml rename to universal7885-common/hardware/samsung/hidl/usb/typeb/android.hardware.usb@1.0-service.a10.xml diff --git a/universal7885-common/hardware/samsung/usb/typeb/service.cpp b/universal7885-common/hardware/samsung/hidl/usb/typeb/service.cpp old mode 100755 new mode 100644 similarity index 100% rename from universal7885-common/hardware/samsung/usb/typeb/service.cpp rename to universal7885-common/hardware/samsung/hidl/usb/typeb/service.cpp diff --git a/universal7885-common/hardware/samsung/usb/typec/Android.bp b/universal7885-common/hardware/samsung/hidl/usb/typec/Android.bp similarity index 70% rename from universal7885-common/hardware/samsung/usb/typec/Android.bp rename to universal7885-common/hardware/samsung/hidl/usb/typec/Android.bp index addae3b..d58539a 100644 --- a/universal7885-common/hardware/samsung/usb/typec/Android.bp +++ b/universal7885-common/hardware/samsung/hidl/usb/typec/Android.bp @@ -1,5 +1,6 @@ // // Copyright (C) 2017 The Android Open Source Project +// Copyright (C) 2022 The LineageOS Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,24 +13,29 @@ // 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. + cc_binary { - name: "android.hardware.usb@1.1-service.exynos7885", + name: "android.hardware.usb@1.3-service.samsung", relative_install_path: "hw", - init_rc: ["android.hardware.usb@1.1-service.exynos7885.rc"], - srcs: ["service.cpp", "Usb.cpp"], - cflags: [ - "-Wall", - "-Werror", + init_rc: ["android.hardware.usb@1.3-service.samsung.rc"], + vintf_fragments: [ + "android.hardware.usb@1.3-service.samsung.xml", ], + srcs: ["service.cpp", "Usb.cpp"], + cflags: ["-Wall", "-Werror"], shared_libs: [ "libbase", + "libbinder", "libhidlbase", "liblog", "libutils", "libhardware", "android.hardware.usb@1.0", "android.hardware.usb@1.1", + "android.hardware.usb@1.2", + "android.hardware.usb@1.3", "libcutils", + "libbinder_ndk", ], proprietary: true, } diff --git a/universal7885-common/hardware/samsung/usb/typec/Usb.cpp b/universal7885-common/hardware/samsung/hidl/usb/typec/Usb.cpp similarity index 59% rename from universal7885-common/hardware/samsung/usb/typec/Usb.cpp rename to universal7885-common/hardware/samsung/hidl/usb/typec/Usb.cpp index d68b57b..2498e42 100644 --- a/universal7885-common/hardware/samsung/usb/typec/Usb.cpp +++ b/universal7885-common/hardware/samsung/hidl/usb/typec/Usb.cpp @@ -1,5 +1,6 @@ /* - * Copyright (C) 2017 The Android Open Source Project + * Copyright (C) 2020 The Android Open Source Project + * Copyright (C) 2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,9 +15,10 @@ * limitations under the License. */ -#define LOG_TAG "android.hardware.usb@1.1-service.typec" +#define LOG_TAG "android.hardware.usb@1.3-service.samsung" #include +#include #include #include #include @@ -35,64 +37,121 @@ #include "Usb.h" +using android::base::GetProperty; + namespace android { namespace hardware { namespace usb { -namespace V1_1 { +namespace V1_3 { namespace implementation { -const char GOOGLE_USB_VENDOR_ID_STR[] = "18d1"; -const char GOOGLE_USBC_35_ADAPTER_UNPLUGGED_ID_STR[] = "5029"; +Return Usb::enableUsbDataSignal(bool enable) { + bool result = true; + + ALOGI("Userspace turn %s USB data signaling", enable ? "on" : "off"); + + if(enable) { + if (!WriteStringToFile("1", USB_DATA_PATH)) { + ALOGE("Not able to turn on usb connection notification"); + result = false; + } + } + else { + if (!WriteStringToFile("0", USB_DATA_PATH)) { + ALOGE("Not able to turn off usb connection notification"); + result = false; + } + } + + return result; +} // Set by the signal handler to destroy the thread volatile bool destroyThread; -static void checkUsbDeviceAutoSuspend(const std::string& devicePath); +bool moistureDetectionEnabled = true; +constexpr char kContaminantDetectionPath[] = "/sys/devices/virtual/sec/ccic/water"; +constexpr char kTypecPath[] = "/sys/class/typec"; +constexpr char kDisableContaminantDetection[] = "vendor.usb.contaminantdisable"; -static int32_t readFile(const std::string& filename, std::string* contents) { - FILE* fp; +int32_t readFile(const std::string &filename, std::string *contents) { + FILE *fp; ssize_t read = 0; - char* line = NULL; + char *line = NULL; size_t len = 0; fp = fopen(filename.c_str(), "r"); if (fp != NULL) { if ((read = getline(&line, &len, fp)) != -1) { - char* pos; - if ((pos = strchr(line, '\n')) != NULL) *pos = '\0'; + char *pos; + if ((pos = strchr(line, '\n')) != NULL) + *pos = '\0'; *contents = line; } free(line); fclose(fp); return 0; } else { - ALOGE("fopen failed in readFile %s, errno=%d", filename.c_str(), errno); + ALOGE("fopen failed"); } return -1; } -static int32_t writeFile(const std::string& filename, const std::string& contents) { - FILE* fp; - int ret; +int32_t writeFile(const std::string &filename, const std::string &contents) { + FILE *fp; + std::string written; fp = fopen(filename.c_str(), "w"); if (fp != NULL) { - ret = fputs(contents.c_str(), fp); + // FAILURE RETRY + int ret = fputs(contents.c_str(), fp); fclose(fp); - if (ret == EOF) { - ALOGE("fputs failed in writeFile %s", filename.c_str()); - return -1; - } - return 0; - } else { - ALOGE("fopen failed in writeFile %s, errno=%d", filename.c_str(), errno); + if ((ret != EOF) && !readFile(filename, &written) && written == contents) + return 0; } - return -1; } -std::string appendRoleNodeHelper(const std::string& portName, PortRoleType type) { +Status queryMoistureDetectionStatus(hidl_vec *currentPortStatus_1_2) { + std::string enabled, status, path, DetectedPath; + + if (currentPortStatus_1_2 == NULL || currentPortStatus_1_2->size() == 0) { + ALOGE("currentPortStatus_1_2 is not available"); + return Status::ERROR; + } + + (*currentPortStatus_1_2)[0].supportedContaminantProtectionModes = 0; + (*currentPortStatus_1_2)[0].supportedContaminantProtectionModes |= + V1_2::ContaminantProtectionMode::FORCE_SINK; + (*currentPortStatus_1_2)[0].contaminantProtectionStatus = V1_2::ContaminantProtectionStatus::NONE; + (*currentPortStatus_1_2)[0].contaminantDetectionStatus = V1_2::ContaminantDetectionStatus::DISABLED; + (*currentPortStatus_1_2)[0].supportsEnableContaminantPresenceDetection = true; + (*currentPortStatus_1_2)[0].supportsEnableContaminantPresenceProtection = false; + + if (moistureDetectionEnabled) { + if (readFile(kContaminantDetectionPath, &status)) { + ALOGE("Failed to open moisture_detected"); + return Status::ERROR; + } + if (status == "1") { + (*currentPortStatus_1_2)[0].contaminantDetectionStatus = + V1_2::ContaminantDetectionStatus::DETECTED; + (*currentPortStatus_1_2)[0].contaminantProtectionStatus = + V1_2::ContaminantProtectionStatus::FORCE_SINK; + } else + (*currentPortStatus_1_2)[0].contaminantDetectionStatus = + V1_2::ContaminantDetectionStatus::NOT_DETECTED; + } + + ALOGI("ContaminantDetectionStatus:%d ContaminantProtectionStatus:%d", + (*currentPortStatus_1_2)[0].contaminantDetectionStatus, + (*currentPortStatus_1_2)[0].contaminantProtectionStatus); + + return Status::SUCCESS; +} + +std::string appendRoleNodeHelper(const std::string &portName, PortRoleType type) { std::string node("/sys/class/typec/" + portName); switch (type) { @@ -114,16 +173,20 @@ std::string convertRoletoString(PortRole role) { else if (role.role == static_cast(PortPowerRole::SINK)) return "sink"; } else if (role.type == PortRoleType::DATA_ROLE) { - if (role.role == static_cast(PortDataRole::HOST)) return "host"; - if (role.role == static_cast(PortDataRole::DEVICE)) return "device"; + if (role.role == static_cast(PortDataRole::HOST)) + return "host"; + if (role.role == static_cast(PortDataRole::DEVICE)) + return "device"; } else if (role.type == PortRoleType::MODE) { - if (role.role == static_cast(PortMode_1_1::UFP)) return "sink"; - if (role.role == static_cast(PortMode_1_1::DFP)) return "source"; + if (role.role == static_cast(PortMode_1_1::UFP)) + return "sink"; + if (role.role == static_cast(PortMode_1_1::DFP)) + return "source"; } return "none"; } -void extractRole(std::string* roleName) { +void extractRole(std::string *roleName) { std::size_t first, last; first = roleName->find("["); @@ -134,16 +197,17 @@ void extractRole(std::string* roleName) { } } -void switchToDrp(const std::string& portName) { +void switchToDrp(const std::string &portName) { std::string filename = appendRoleNodeHelper(std::string(portName.c_str()), PortRoleType::MODE); - FILE* fp; + FILE *fp; if (filename != "") { fp = fopen(filename.c_str(), "w"); if (fp != NULL) { int ret = fputs("dual", fp); fclose(fp); - if (ret == EOF) ALOGE("Fatal: Error while switching back to drp"); + if (ret == EOF) + ALOGE("Fatal: Error while switching back to drp"); } else { ALOGE("Fatal: Cannot open file to switch back to drp"); } @@ -152,10 +216,10 @@ void switchToDrp(const std::string& portName) { } } -bool switchMode(const hidl_string& portName, const PortRole& newRole, struct Usb* usb) { +bool switchMode(const hidl_string &portName, const PortRole &newRole, struct Usb *usb) { std::string filename = appendRoleNodeHelper(std::string(portName.c_str()), newRole.type); std::string written; - FILE* fp; + FILE *fp; bool roleSwitch = false; if (filename == "") { @@ -186,7 +250,7 @@ bool switchMode(const hidl_string& portName, const PortRole& newRole, struct Usb // There are no uevent signals which implies role swap timed out. if (err == ETIMEDOUT) { ALOGI("uevents wait timedout"); - // Sanity check. + // Validity check. } else if (!usb->mPartnerUp) { goto wait_again; // Role switch succeeded since usb->mPartnerUp is true. @@ -199,7 +263,8 @@ bool switchMode(const hidl_string& portName, const PortRole& newRole, struct Usb pthread_mutex_unlock(&usb->mPartnerLock); } - if (!roleSwitch) switchToDrp(std::string(portName.c_str())); + if (!roleSwitch) + switchToDrp(std::string(portName.c_str())); return roleSwitch; } @@ -228,10 +293,10 @@ Usb::Usb() } } -Return Usb::switchRole(const hidl_string& portName, const PortRole& newRole) { +Return Usb::switchRole(const hidl_string &portName, const V1_0::PortRole &newRole) { std::string filename = appendRoleNodeHelper(std::string(portName.c_str()), newRole.type); std::string written; - FILE* fp; + FILE *fp; bool roleSwitch = false; if (filename == "") { @@ -269,8 +334,9 @@ Return Usb::switchRole(const hidl_string& portName, const PortRole& newRol pthread_mutex_lock(&mLock); if (mCallback_1_0 != NULL) { Return ret = mCallback_1_0->notifyRoleSwitchStatus( - portName, newRole, roleSwitch ? Status::SUCCESS : Status::ERROR); - if (!ret.isOk()) ALOGE("RoleSwitchStatus error %s", ret.description().c_str()); + portName, newRole, roleSwitch ? Status::SUCCESS : Status::ERROR); + if (!ret.isOk()) + ALOGE("RoleSwitchStatus error %s", ret.description().c_str()); } else { ALOGE("Not notifying the userspace. Callback is not set"); } @@ -280,7 +346,7 @@ Return Usb::switchRole(const hidl_string& portName, const PortRole& newRol return Void(); } -Status getAccessoryConnected(const std::string& portName, std::string* accessory) { +Status getAccessoryConnected(const std::string &portName, std::string *accessory) { std::string filename = "/sys/class/typec/" + portName + "-partner/accessory_mode"; if (readFile(filename, accessory)) { @@ -291,8 +357,8 @@ Status getAccessoryConnected(const std::string& portName, std::string* accessory return Status::SUCCESS; } -Status getCurrentRoleHelper(const std::string& portName, bool connected, PortRoleType type, - uint32_t* currentRole) { +Status getCurrentRoleHelper(const std::string &portName, bool connected, PortRoleType type, + uint32_t *currentRole) { std::string filename; std::string roleName; std::string accessory; @@ -312,7 +378,8 @@ Status getCurrentRoleHelper(const std::string& portName, bool connected, PortRol return Status::ERROR; } - if (!connected) return Status::SUCCESS; + if (!connected) + return Status::SUCCESS; if (type == PortRoleType::MODE) { if (getAccessoryConnected(portName, &accessory) != Status::SUCCESS) { @@ -350,7 +417,7 @@ Status getCurrentRoleHelper(const std::string& portName, bool connected, PortRol *currentRole = static_cast(PortMode_1_1::UFP); } else if (roleName != "none") { /* case for none has already been addressed. - * so we check if the role isnt none. + * so we check if the role isn't none. */ return Status::UNRECOGNIZED_ROLE; } @@ -358,18 +425,18 @@ Status getCurrentRoleHelper(const std::string& portName, bool connected, PortRol return Status::SUCCESS; } -Status getTypeCPortNamesHelper(std::unordered_map* names) { - DIR* dp; +Status getTypeCPortNamesHelper(std::unordered_map *names) { + DIR *dp; - dp = opendir("/sys/class/typec"); + dp = opendir(kTypecPath); if (dp != NULL) { - struct dirent* ep; + struct dirent *ep; while ((ep = readdir(dp))) { if (ep->d_type == DT_LNK) { if (std::string::npos == std::string(ep->d_name).find("-partner")) { std::unordered_map::const_iterator portName = - names->find(ep->d_name); + names->find(ep->d_name); if (portName == names->end()) { names->insert({ep->d_name, false}); } @@ -386,7 +453,7 @@ Status getTypeCPortNamesHelper(std::unordered_map* names) { return Status::ERROR; } -bool canSwitchRoleHelper(const std::string& portName, PortRoleType /*type*/) { +bool canSwitchRoleHelper(const std::string &portName, PortRoleType /*type*/) { std::string filename = "/sys/class/typec/" + portName + "-partner/supports_usb_power_delivery"; std::string supportsPD; @@ -404,65 +471,75 @@ bool canSwitchRoleHelper(const std::string& portName, PortRoleType /*type*/) { * The caller of this method would reconstruct the V1_0::PortStatus * object if required. */ -Status getPortStatusHelper(hidl_vec* currentPortStatus_1_1, bool V1_0) { +Status getPortStatusHelper(hidl_vec *currentPortStatus_1_2, HALVersion version) { std::unordered_map names; Status result = getTypeCPortNamesHelper(&names); int i = -1; if (result == Status::SUCCESS) { - currentPortStatus_1_1->resize(names.size()); + currentPortStatus_1_2->resize(names.size()); for (std::pair port : names) { i++; ALOGI("%s", port.first.c_str()); - (*currentPortStatus_1_1)[i].status.portName = port.first; + (*currentPortStatus_1_2)[i].status_1_1.status.portName = port.first; uint32_t currentRole; if (getCurrentRoleHelper(port.first, port.second, PortRoleType::POWER_ROLE, ¤tRole) == Status::SUCCESS) { - (*currentPortStatus_1_1)[i].status.currentPowerRole = - static_cast(currentRole); + (*currentPortStatus_1_2)[i].status_1_1.status.currentPowerRole = + static_cast(currentRole); } else { - ALOGE("Error while retreiving portNames"); + ALOGE("Error while retrieving portNames"); goto done; } if (getCurrentRoleHelper(port.first, port.second, PortRoleType::DATA_ROLE, ¤tRole) == Status::SUCCESS) { - (*currentPortStatus_1_1)[i].status.currentDataRole = - static_cast(currentRole); + (*currentPortStatus_1_2)[i].status_1_1.status.currentDataRole = + static_cast(currentRole); } else { - ALOGE("Error while retreiving current port role"); + ALOGE("Error while retrieving current port role"); goto done; } if (getCurrentRoleHelper(port.first, port.second, PortRoleType::MODE, ¤tRole) == Status::SUCCESS) { - (*currentPortStatus_1_1)[i].currentMode = static_cast(currentRole); - (*currentPortStatus_1_1)[i].status.currentMode = - static_cast(currentRole); + (*currentPortStatus_1_2)[i].status_1_1.currentMode = + static_cast(currentRole); + (*currentPortStatus_1_2)[i].status_1_1.status.currentMode = + static_cast(currentRole); } else { - ALOGE("Error while retreiving current data role"); + ALOGE("Error while retrieving current data role"); goto done; } - (*currentPortStatus_1_1)[i].status.canChangeMode = true; - (*currentPortStatus_1_1)[i].status.canChangeDataRole = - port.second ? canSwitchRoleHelper(port.first, PortRoleType::DATA_ROLE) : false; - (*currentPortStatus_1_1)[i].status.canChangePowerRole = - port.second ? canSwitchRoleHelper(port.first, PortRoleType::POWER_ROLE) : false; + (*currentPortStatus_1_2)[i].status_1_1.status.canChangeMode = true; + (*currentPortStatus_1_2)[i].status_1_1.status.canChangeDataRole = + port.second ? canSwitchRoleHelper(port.first, PortRoleType::DATA_ROLE) : false; + (*currentPortStatus_1_2)[i].status_1_1.status.canChangePowerRole = + port.second ? canSwitchRoleHelper(port.first, PortRoleType::POWER_ROLE) : false; - ALOGI("connected:%d canChangeMode:%d canChagedata:%d canChangePower:%d", port.second, - (*currentPortStatus_1_1)[i].status.canChangeMode, - (*currentPortStatus_1_1)[i].status.canChangeDataRole, - (*currentPortStatus_1_1)[i].status.canChangePowerRole); - - if (V1_0) { - (*currentPortStatus_1_1)[i].status.supportedModes = V1_0::PortMode::DFP; + if (version == HALVersion::V1_0) { + ALOGI("HAL version V1_0"); + (*currentPortStatus_1_2)[i].status_1_1.status.supportedModes = V1_0::PortMode::DRP; } else { - (*currentPortStatus_1_1)[i].supportedModes = PortMode_1_1::UFP | PortMode_1_1::DFP; - (*currentPortStatus_1_1)[i].status.supportedModes = V1_0::PortMode::NONE; - (*currentPortStatus_1_1)[i].status.currentMode = V1_0::PortMode::NONE; + if (version == HALVersion::V1_1) + ALOGI("HAL version V1_1"); + else + ALOGI("HAL version V1_2"); + (*currentPortStatus_1_2)[i].status_1_1.supportedModes = 0 | PortMode_1_1::DRP; + (*currentPortStatus_1_2)[i].status_1_1.status.supportedModes = V1_0::PortMode::NONE; + (*currentPortStatus_1_2)[i].status_1_1.status.currentMode = V1_0::PortMode::NONE; } + + ALOGI( + "%d:%s connected:%d canChangeMode:%d canChagedata:%d canChangePower:%d " + "supportedModes:%d", + i, port.first.c_str(), port.second, + (*currentPortStatus_1_2)[i].status_1_1.status.canChangeMode, + (*currentPortStatus_1_2)[i].status_1_1.status.canChangeDataRole, + (*currentPortStatus_1_2)[i].status_1_1.status.canChangePowerRole, + (*currentPortStatus_1_2)[i].status_1_1.supportedModes); } return Status::SUCCESS; } @@ -470,51 +547,90 @@ done: return Status::ERROR; } -Return Usb::queryPortStatus() { - hidl_vec currentPortStatus_1_1; +void queryVersionHelper(android::hardware::usb::V1_3::implementation::Usb *usb, + hidl_vec *currentPortStatus_1_2) { + hidl_vec currentPortStatus_1_1; hidl_vec currentPortStatus; Status status; - sp callback_V1_1 = IUsbCallback::castFrom(mCallback_1_0); + sp callback_V1_1 = V1_1::IUsbCallback::castFrom(usb->mCallback_1_0); + sp callback_V1_2 = IUsbCallback::castFrom(usb->mCallback_1_0); - pthread_mutex_lock(&mLock); - if (mCallback_1_0 != NULL) { - if (callback_V1_1 != NULL) { - status = getPortStatusHelper(¤tPortStatus_1_1, false); + pthread_mutex_lock(&usb->mLock); + if (usb->mCallback_1_0 != NULL) { + if (callback_V1_2 != NULL) { + status = getPortStatusHelper(currentPortStatus_1_2, HALVersion::V1_2); + if (status == Status::SUCCESS) + queryMoistureDetectionStatus(currentPortStatus_1_2); + } else if (callback_V1_1 != NULL) { + status = getPortStatusHelper(currentPortStatus_1_2, HALVersion::V1_1); + currentPortStatus_1_1.resize(currentPortStatus_1_2->size()); + for (unsigned long i = 0; i < currentPortStatus_1_2->size(); i++) + currentPortStatus_1_1[i] = (*currentPortStatus_1_2)[i].status_1_1; } else { - status = getPortStatusHelper(¤tPortStatus_1_1, true); - currentPortStatus.resize(currentPortStatus_1_1.size()); - for (unsigned long i = 0; i < currentPortStatus_1_1.size(); i++) - currentPortStatus[i] = currentPortStatus_1_1[i].status; + status = getPortStatusHelper(currentPortStatus_1_2, HALVersion::V1_0); + currentPortStatus.resize(currentPortStatus_1_2->size()); + for (unsigned long i = 0; i < currentPortStatus_1_2->size(); i++) + currentPortStatus[i] = (*currentPortStatus_1_2)[i].status_1_1.status; } Return ret; - if (callback_V1_1 != NULL) + if (callback_V1_2 != NULL) + ret = callback_V1_2->notifyPortStatusChange_1_2(*currentPortStatus_1_2, status); + else if (callback_V1_1 != NULL) ret = callback_V1_1->notifyPortStatusChange_1_1(currentPortStatus_1_1, status); else - ret = mCallback_1_0->notifyPortStatusChange(currentPortStatus, status); + ret = usb->mCallback_1_0->notifyPortStatusChange(currentPortStatus, status); - if (!ret.isOk()) ALOGE("queryPortStatus_1_1 error %s", ret.description().c_str()); + if (!ret.isOk()) + ALOGE("queryPortStatus_1_2 error %s", ret.description().c_str()); } else { ALOGI("Notifying userspace skipped. Callback is NULL"); } - pthread_mutex_unlock(&mLock); + pthread_mutex_unlock(&usb->mLock); +} +Return Usb::queryPortStatus() { + hidl_vec currentPortStatus_1_2; + + queryVersionHelper(this, ¤tPortStatus_1_2); + return Void(); +} + +Return Usb::enableContaminantPresenceDetection(const hidl_string & /*portName*/, + bool enable) { + std::string disable = GetProperty(kDisableContaminantDetection, ""); + + if (disable != "true") + moistureDetectionEnabled = enable; + + hidl_vec currentPortStatus_1_2; + + queryVersionHelper(this, ¤tPortStatus_1_2); + return Void(); +} + +Return Usb::enableContaminantPresenceProtection(const hidl_string & /*portName*/, + bool /*enable*/) { + hidl_vec currentPortStatus_1_2; + + queryVersionHelper(this, ¤tPortStatus_1_2); return Void(); } struct data { int uevent_fd; - android::hardware::usb::V1_1::implementation::Usb* usb; + android::hardware::usb::V1_3::implementation::Usb *usb; }; -static void uevent_event(uint32_t /*epevents*/, struct data* payload) { +static void uevent_event(uint32_t /*epevents*/, struct data *payload) { char msg[UEVENT_MSG_LEN + 2]; - char* cp; + char *cp; int n; n = uevent_kernel_multicast_recv(payload->uevent_fd, msg, UEVENT_MSG_LEN); - if (n <= 0) return; + if (n <= 0) + return; if (n >= UEVENT_MSG_LEN) /* overflow -- discard */ return; @@ -523,60 +639,29 @@ static void uevent_event(uint32_t /*epevents*/, struct data* payload) { cp = msg; while (*cp) { - std::cmatch match; if (std::regex_match(cp, std::regex("(add)(.*)(-partner)"))) { ALOGI("partner added"); pthread_mutex_lock(&payload->usb->mPartnerLock); payload->usb->mPartnerUp = true; pthread_cond_signal(&payload->usb->mPartnerCV); pthread_mutex_unlock(&payload->usb->mPartnerLock); - } else if (!strncmp(cp, "DEVTYPE=typec_", strlen("DEVTYPE=typec_"))) { - hidl_vec currentPortStatus_1_1; - ALOGI("uevent received %s", cp); - pthread_mutex_lock(&payload->usb->mLock); - if (payload->usb->mCallback_1_0 != NULL) { - sp callback_V1_1 = - IUsbCallback::castFrom(payload->usb->mCallback_1_0); - Return ret; - - // V1_1 callback - if (callback_V1_1 != NULL) { - Status status = getPortStatusHelper(¤tPortStatus_1_1, false); - ret = callback_V1_1->notifyPortStatusChange_1_1(currentPortStatus_1_1, status); - } else { // V1_0 callback - Status status = getPortStatusHelper(¤tPortStatus_1_1, true); - - /* - * Copying the result from getPortStatusHelper - * into V1_0::PortStatus to pass back through - * the V1_0 callback object. - */ - hidl_vec currentPortStatus; - currentPortStatus.resize(currentPortStatus_1_1.size()); - for (unsigned long i = 0; i < currentPortStatus_1_1.size(); i++) - currentPortStatus[i] = currentPortStatus_1_1[i].status; - - ret = payload->usb->mCallback_1_0->notifyPortStatusChange(currentPortStatus, - status); - } - if (!ret.isOk()) ALOGE("error %s", ret.description().c_str()); - } else { - ALOGI("Notifying userspace skipped. Callback is NULL"); - } - pthread_mutex_unlock(&payload->usb->mLock); + } else if (!strncmp(cp, "DEVTYPE=typec_", strlen("DEVTYPE=typec_")) || + !strncmp(cp, "CCIC=WATER", strlen("CCIC=WATER"))) { + hidl_vec currentPortStatus_1_2; + queryVersionHelper(payload->usb, ¤tPortStatus_1_2); // Role switch is not in progress and port is in disconnected state if (!pthread_mutex_trylock(&payload->usb->mRoleSwitchLock)) { - for (unsigned long i = 0; i < currentPortStatus_1_1.size(); i++) { - DIR* dp = opendir( - std::string( - "/sys/class/typec/" + - std::string(currentPortStatus_1_1[i].status.portName.c_str()) + - "-partner") + for (unsigned long i = 0; i < currentPortStatus_1_2.size(); i++) { + DIR *dp = + opendir(std::string("/sys/class/typec/" + + std::string(currentPortStatus_1_2[i] + .status_1_1.status.portName.c_str()) + + "-partner") .c_str()); if (dp == NULL) { // PortRole role = {.role = static_cast(PortMode::UFP)}; - switchToDrp(currentPortStatus_1_1[i].status.portName); + switchToDrp(currentPortStatus_1_2[i].status_1_1.status.portName); } else { closedir(dp); } @@ -584,23 +669,14 @@ static void uevent_event(uint32_t /*epevents*/, struct data* payload) { pthread_mutex_unlock(&payload->usb->mRoleSwitchLock); } break; - } else if (std::regex_match(cp, match, - std::regex("add@(/devices/soc/a800000\\.ssusb/a800000\\.dwc3/" - "xhci-hcd\\.0\\.auto/" - "usb\\d/\\d-\\d)/.*"))) { - if (match.size() == 2) { - std::csub_match submatch = match[1]; - checkUsbDeviceAutoSuspend("/sys" + submatch.str()); - } } - /* advance to after the next \0 */ while (*cp++) { } } } -void* work(void* param) { +void *work(void *param) { int epoll_fd, uevent_fd; struct epoll_event ev; int nevents = 0; @@ -616,12 +692,12 @@ void* work(void* param) { } payload.uevent_fd = uevent_fd; - payload.usb = (android::hardware::usb::V1_1::implementation::Usb*)param; + payload.usb = (android::hardware::usb::V1_3::implementation::Usb *)param; fcntl(uevent_fd, F_SETFL, O_NONBLOCK); ev.events = EPOLLIN; - ev.data.ptr = (void*)uevent_event; + ev.data.ptr = (void *)uevent_event; epoll_fd = epoll_create(64); if (epoll_fd == -1) { @@ -639,14 +715,15 @@ void* work(void* param) { nevents = epoll_wait(epoll_fd, events, 64, -1); if (nevents == -1) { - if (errno == EINTR) continue; + if (errno == EINTR) + continue; ALOGE("usb epoll_wait failed; errno=%d", errno); break; } for (int n = 0; n < nevents; ++n) { if (events[n].data.ptr) - (*(void (*)(int, struct data* payload))events[n].data.ptr)(events[n].events, + (*(void (*)(int, struct data *payload))events[n].data.ptr)(events[n].events, &payload); } } @@ -655,7 +732,8 @@ void* work(void* param) { error: close(uevent_fd); - if (epoll_fd >= 0) close(epoll_fd); + if (epoll_fd >= 0) + close(epoll_fd); return NULL; } @@ -669,11 +747,16 @@ void sighandler(int sig) { signal(SIGUSR1, sighandler); } -Return Usb::setCallback(const sp& callback) { - sp callback_V1_1 = IUsbCallback::castFrom(callback); +Return Usb::setCallback(const sp &callback) { + sp callback_V1_1 = V1_1::IUsbCallback::castFrom(callback); + sp callback_V1_2 = IUsbCallback::castFrom(callback); - if (callback != NULL) - if (callback_V1_1 == NULL) ALOGI("Registering 1.0 callback"); + if (callback != NULL) { + if (callback_V1_2 != NULL) + ALOGI("Registering 1.2 callback"); + else if (callback_V1_1 != NULL) + ALOGI("Registering 1.1 callback"); + } pthread_mutex_lock(&mLock); /* @@ -723,46 +806,8 @@ Return Usb::setCallback(const sp& callback) { return Void(); } -/* - * whitelisting USB device idProduct and idVendor to allow auto suspend. - */ -static bool canProductAutoSuspend(const std::string& deviceIdVendor, - const std::string& deviceIdProduct) { - if (deviceIdVendor == GOOGLE_USB_VENDOR_ID_STR && - deviceIdProduct == GOOGLE_USBC_35_ADAPTER_UNPLUGGED_ID_STR) { - return true; - } - return false; -} - -static bool canUsbDeviceAutoSuspend(const std::string& devicePath) { - std::string deviceIdVendor; - std::string deviceIdProduct; - readFile(devicePath + "/idVendor", &deviceIdVendor); - readFile(devicePath + "/idProduct", &deviceIdProduct); - - // deviceIdVendor and deviceIdProduct will be empty strings if readFile fails - return canProductAutoSuspend(deviceIdVendor, deviceIdProduct); -} - -/* - * function to consume USB device plugin events (on receiving a - * USB device path string), and enable autosupend on the USB device if - * necessary. - */ -void checkUsbDeviceAutoSuspend(const std::string& devicePath) { - /* - * Currently we only actively enable devices that should be autosuspended, and leave others - * to the defualt. - */ - if (canUsbDeviceAutoSuspend(devicePath)) { - ALOGI("auto suspend usb device %s", devicePath.c_str()); - writeFile(devicePath + "/power/control", "auto"); - } -} - } // namespace implementation -} // namespace V1_1 +} // namespace V1_3 } // namespace usb } // namespace hardware } // namespace android diff --git a/universal7885-common/hardware/samsung/usb/typec/Usb.h b/universal7885-common/hardware/samsung/hidl/usb/typec/Usb.h similarity index 53% rename from universal7885-common/hardware/samsung/usb/typec/Usb.h rename to universal7885-common/hardware/samsung/hidl/usb/typec/Usb.h index daa4977..8e51297 100644 --- a/universal7885-common/hardware/samsung/usb/typec/Usb.h +++ b/universal7885-common/hardware/samsung/hidl/usb/typec/Usb.h @@ -1,9 +1,25 @@ -#ifndef ANDROID_HARDWARE_USB_V1_1_USB_H -#define ANDROID_HARDWARE_USB_V1_1_USB_H +/* + * Copyright (C) 2020 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 -#include -#include +#pragma once + +#include +#include +#include +#include #include #include @@ -17,34 +33,51 @@ namespace android { namespace hardware { namespace usb { -namespace V1_1 { +namespace V1_3 { namespace implementation { -using ::android::sp; +using ::android::base::WriteStringToFile; +using ::android::base::ReadFileToString; using ::android::hardware::hidl_array; using ::android::hardware::hidl_memory; using ::android::hardware::hidl_string; using ::android::hardware::hidl_vec; using ::android::hardware::Return; using ::android::hardware::Void; +using ::android::hardware::usb::V1_0::PortRole; +using ::android::hardware::usb::V1_0::PortRoleType; using ::android::hardware::usb::V1_0::PortDataRole; using ::android::hardware::usb::V1_0::PortPowerRole; using ::android::hardware::usb::V1_0::PortRole; using ::android::hardware::usb::V1_0::PortRoleType; using ::android::hardware::usb::V1_0::Status; -using ::android::hardware::usb::V1_1::IUsb; -using ::android::hardware::usb::V1_1::IUsbCallback; +using ::android::hardware::usb::V1_3::IUsb; +using ::android::hardware::usb::V1_2::IUsbCallback; +using ::android::hardware::usb::V1_2::PortStatus; using ::android::hardware::usb::V1_1::PortMode_1_1; using ::android::hardware::usb::V1_1::PortStatus_1_1; using ::android::hidl::base::V1_0::DebugInfo; using ::android::hidl::base::V1_0::IBase; +using ::android::sp; + +enum class HALVersion{ + V1_0, + V1_1, + V1_2, + V1_3 +}; + +#define USB_DATA_PATH "/sys/devices/virtual/usb_notify/usb_control/usb_data_enabled" struct Usb : public IUsb { Usb(); - Return switchRole(const hidl_string& portName, const PortRole& role) override; + Return switchRole(const hidl_string &portName, const PortRole &role) override; Return setCallback(const sp& callback) override; Return queryPortStatus() override; + Return enableContaminantPresenceDetection(const hidl_string &portName, bool enable); + Return enableContaminantPresenceProtection(const hidl_string &portName, bool enable); + Return enableUsbDataSignal(bool enable) override; sp mCallback_1_0; // Protects mCallback variable @@ -63,9 +96,7 @@ struct Usb : public IUsb { }; } // namespace implementation -} // namespace V1_1 +} // namespace V1_3 } // namespace usb } // namespace hardware } // namespace android - -#endif // ANDROID_HARDWARE_USB_V1_1_USB_H diff --git a/universal7885-common/hardware/samsung/hidl/usb/typec/android.hardware.usb@1.3-service.samsung.rc b/universal7885-common/hardware/samsung/hidl/usb/typec/android.hardware.usb@1.3-service.samsung.rc new file mode 100644 index 0000000..5fb3194 --- /dev/null +++ b/universal7885-common/hardware/samsung/hidl/usb/typec/android.hardware.usb@1.3-service.samsung.rc @@ -0,0 +1,16 @@ +service vendor.usb-hal-1-3 /vendor/bin/hw/android.hardware.usb@1.3-service.samsung + class hal + user system + group system shell mtp wakelock + capabilities WAKE_ALARM BLOCK_SUSPEND + +on post-fs + chown root system /sys/class/typec/port0/power_role + chown root system /sys/class/typec/port0/data_role + chown root system /sys/class/typec/port0/port_type + chown root system /sys/devices/virtual/sec/ccic/water + chown root system /sys/devices/virtual/usb_notify/usb_control/usb_data_enabled + chmod 664 /sys/class/typec/port0/power_role + chmod 664 /sys/class/typec/port0/data_role + chmod 664 /sys/class/typec/port0/port_type + chmod 664 /sys/devices/virtual/usb_notify/usb_control/usb_data_enabled diff --git a/universal7885-common/hardware/samsung/hidl/usb/typec/android.hardware.usb@1.3-service.samsung.xml b/universal7885-common/hardware/samsung/hidl/usb/typec/android.hardware.usb@1.3-service.samsung.xml new file mode 100644 index 0000000..cd54268 --- /dev/null +++ b/universal7885-common/hardware/samsung/hidl/usb/typec/android.hardware.usb@1.3-service.samsung.xml @@ -0,0 +1,12 @@ + + + android.hardware.usb + hwbinder + 1.3 + + IUsb + default + + + + diff --git a/universal7885-common/hardware/samsung/usb/typec/service.cpp b/universal7885-common/hardware/samsung/hidl/usb/typec/service.cpp similarity index 83% rename from universal7885-common/hardware/samsung/usb/typec/service.cpp rename to universal7885-common/hardware/samsung/hidl/usb/typec/service.cpp index 7b2bbf9..33bd645 100644 --- a/universal7885-common/hardware/samsung/usb/typec/service.cpp +++ b/universal7885-common/hardware/samsung/hidl/usb/typec/service.cpp @@ -1,5 +1,6 @@ /* - * Copyright (C) 2016 The Android Open Source Project + * Copyright (C) 2018 The Android Open Source Project + * Copyright (C) 2022 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +15,7 @@ * limitations under the License. */ -#define LOG_TAG "android.hardware.usb@1.1-service.exynos7885" +#define LOG_TAG "android.hardware.usb@1.3-service.samsung" #include #include "Usb.h" @@ -26,8 +27,8 @@ using android::hardware::configureRpcThreadpool; using android::hardware::joinRpcThreadpool; // Generated HIDL files -using android::hardware::usb::V1_1::IUsb; -using android::hardware::usb::V1_1::implementation::Usb; +using android::hardware::usb::V1_3::IUsb; +using android::hardware::usb::V1_3::implementation::Usb; using android::OK; using android::status_t; diff --git a/universal7885-common/hardware/samsung/usb/typec/android.hardware.usb@1.1-service.exynos7885.rc b/universal7885-common/hardware/samsung/usb/typec/android.hardware.usb@1.1-service.exynos7885.rc deleted file mode 100644 index b72ea88..0000000 --- a/universal7885-common/hardware/samsung/usb/typec/android.hardware.usb@1.1-service.exynos7885.rc +++ /dev/null @@ -1,14 +0,0 @@ -service vendor.usb-hal-1-1 /vendor/bin/hw/android.hardware.usb@1.1-service.exynos7885 - interface android.hardware.usb@1.1::IUsb default - interface android.hardware.usb@1.0::IUsb default - class hal - user root - group root system - -on boot - chown root system /sys/class/typec/port0/power_role - chown root system /sys/class/typec/port0/data_role - chown root system /sys/class/typec/port0/port_type - chmod 664 /sys/class/typec/port0/power_role - chmod 664 /sys/class/typec/port0/data_role - chmod 664 /sys/class/typec/port0/port_type diff --git a/universal7885-common/sepolicy/vendor/file_contexts b/universal7885-common/sepolicy/vendor/file_contexts index 90b6162..9420441 100644 --- a/universal7885-common/sepolicy/vendor/file_contexts +++ b/universal7885-common/sepolicy/vendor/file_contexts @@ -209,7 +209,7 @@ /(vendor|system/vendor)/bin/hw/android\.hardware\.drm@[0-9]\.[0-9]-service\.widevine u:object_r:hal_drm_widevine_exec:s0 /(vendor|system/vendor)/bin/hw/android\.hardware\.keymaster@[0-9]\.[0-9]-service\.samsung u:object_r:hal_keymaster_default_exec:s0 /(vendor|system/vendor)/bin/hw/android\.hardware\.nfc@[0-9]\.[0-9]-service\.samsung u:object_r:hal_nfc_default_exec:s0 -/(vendor|system/vendor)/bin/hw/android\.hardware\.usb@[0-9]\.[0-9]-service\.exynos7885 u:object_r:hal_usb_default_exec:s0 +/(vendor|system/vendor)/bin/hw/android\.hardware\.usb@[0-9]\.[0-9]-service\.samsung u:object_r:hal_usb_default_exec:s0 /(vendor|system/vendor)/bin/hw/android\.hardware\.camera\.provider@[0-9]\.[0-9]-service\.exynos7885 u:object_r:hal_camera_default_exec:s0 /(vendor|system/vendor)/bin/hw/android\.hardware\.usb@[0-9]\.[0-9]-service\.a10 u:object_r:hal_usb_default_exec:s0 /(vendor|system/vendor)/bin/hw/vendor\.samsung\.hardware\.gnss@[0-9]\.[0-9]-service u:object_r:hal_gnss_default_exec:s0