usb: Runtime search the i2c path

The paths of sysfs nodes are different on multiple build targets and are
also different between the old kernel (5.10) and newer kernel (6.1).
Since usb HAL code is shared by those targets and different kernel
versions, Runtime search the correct paths.

Bug: 317946158
Change-Id: I676455145232fd71db1578bb7a9801fed7bb327f
Signed-off-by: Kyle Tso <kyletso@google.com>
This commit is contained in:
Kyle Tso 2024-01-16 10:16:14 +08:00 committed by RD Babiera
parent 7d2aafad34
commit 9bafd5f134
4 changed files with 79 additions and 86 deletions

View file

@ -17,7 +17,6 @@
#define LOG_TAG "android.hardware.usb.gadget.aidl-service"
#include "UsbGadget.h"
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/inotify.h>
@ -29,6 +28,7 @@
#include <android-base/properties.h>
#include <aidl/android/frameworks/stats/IStats.h>
#include <pixelusb/I2cHelper.h>
namespace aidl {
namespace android {
@ -37,39 +37,19 @@ namespace usb {
namespace gadget {
using ::android::base::GetBoolProperty;
using ::android::hardware::google::pixel::usb::getI2cClientPath;
using ::android::hardware::google::pixel::usb::kUvcEnabled;
string enabledPath;
constexpr char kHsi2cPath[] = "/sys/devices/platform/10d60000.hsi2c";
constexpr char kI2CPath[] = "/sys/devices/platform/10d60000.hsi2c/i2c-";
constexpr char kAccessoryLimitCurrent[] = "i2c-max77759tcpc/usb_limit_accessory_current";
constexpr char kAccessoryLimitCurrentEnable[] = "i2c-max77759tcpc/usb_limit_accessory_enable";
constexpr char kUpdateSdpEnumTimeout[] = "i2c-max77759tcpc/update_sdp_enum_timeout";
constexpr char kTcpcDevName[] = "i2c-max77759tcpc";
constexpr char kI2cClientId[] = "0025";
constexpr char kAccessoryLimitCurrent[] = "usb_limit_accessory_current";
constexpr char kAccessoryLimitCurrentEnable[] = "usb_limit_accessory_enable";
constexpr char kUpdateSdpEnumTimeout[] = "update_sdp_enum_timeout";
Status getI2cBusHelper(string *name) {
DIR *dp;
dp = opendir(kHsi2cPath);
if (dp != NULL) {
struct dirent *ep;
while ((ep = readdir(dp))) {
if (ep->d_type == DT_DIR) {
if (string::npos != string(ep->d_name).find("i2c-")) {
std::strtok(ep->d_name, "-");
*name = std::strtok(NULL, "-");
}
}
}
closedir(dp);
return Status::SUCCESS;
}
ALOGE("Failed to open %s", kHsi2cPath);
return Status::ERROR;
}
UsbGadget::UsbGadget() : mGadgetIrqPath("") {
UsbGadget::UsbGadget() : mGadgetIrqPath(""),
mI2cClientPath("") {
if (access(OS_DESC_PATH, R_OK) != 0) {
ALOGE("configfs setup not done yet");
abort();
@ -389,14 +369,16 @@ ScopedAStatus UsbGadget::reset(const shared_ptr<IUsbGadgetCallback> &callback,
}
void UsbGadget::updateSdpEnumTimeout() {
string i2c_node, update_sdp_enum_timeout_path;
string update_sdp_enum_timeout_path;
Status status = getI2cBusHelper(&i2c_node);
if (status != Status::SUCCESS) {
ALOGE("%s: Unable to locate i2c bus node", __func__);
if (mI2cClientPath.empty()) {
mI2cClientPath = getI2cClientPath(kHsi2cPath, kTcpcDevName, kI2cClientId);
if (mI2cClientPath.empty()) {
ALOGE("%s: Unable to locate i2c bus node", __func__);
}
}
update_sdp_enum_timeout_path = kI2CPath + i2c_node + "/" + kUpdateSdpEnumTimeout;
update_sdp_enum_timeout_path = mI2cClientPath + kUpdateSdpEnumTimeout;
if (!WriteStringToFile("1", update_sdp_enum_timeout_path)) {
ALOGE("%s: Unable to write to %s.", __func__, update_sdp_enum_timeout_path.c_str());
} else {
@ -494,15 +476,20 @@ ScopedAStatus UsbGadget::setCurrentUsbFunctions(long functions,
std::unique_lock<std::mutex> lk(mLockSetCurrentFunction);
std::string current_usb_power_operation_mode, current_usb_type;
std::string usb_limit_sink_enable;
string accessoryCurrentLimitEnablePath, accessoryCurrentLimitPath, path;
std::string accessoryCurrentLimitEnablePath, accessoryCurrentLimitPath;
mCurrentUsbFunctions = functions;
mCurrentUsbFunctionsApplied = false;
getI2cBusHelper(&path);
accessoryCurrentLimitPath = kI2CPath + path + "/" + kAccessoryLimitCurrent;
accessoryCurrentLimitEnablePath = kI2CPath + path + "/" + kAccessoryLimitCurrentEnable;
if (mI2cClientPath.empty()) {
mI2cClientPath = getI2cClientPath(kHsi2cPath, kTcpcDevName, kI2cClientId);
if (mI2cClientPath.empty()) {
ALOGE("%s: Unable to locate i2c bus node", __func__);
}
}
accessoryCurrentLimitPath = mI2cClientPath + kAccessoryLimitCurrent;
accessoryCurrentLimitEnablePath = mI2cClientPath + kAccessoryLimitCurrentEnable;
// Get the gadget IRQ number before tearDownGadget()
if (mGadgetIrqPath.empty())

View file

@ -118,6 +118,7 @@ struct UsbGadget : public BnUsbGadget {
// Indicates to the kernel that the gadget service is ready and the kernel can
// set SDP timeout to a lower value.
void updateSdpEnumTimeout();
std::string mI2cClientPath;
private:
Status tearDownGadget();

View file

@ -43,8 +43,9 @@
#include <aidl/android/frameworks/stats/IStats.h>
#include <android_hardware_usb_flags.h>
#include <pixelusb/UsbGadgetAidlCommon.h>
#include <pixelstats/StatsHelper.h>
#include <pixelusb/I2cHelper.h>
#include <pixelusb/UsbGadgetAidlCommon.h>
namespace usb_flags = android::hardware::usb::flags;
@ -55,6 +56,7 @@ using android::base::Trim;
using android::hardware::google::pixel::getStatsService;
using android::hardware::google::pixel::PixelAtoms::VendorUsbPortOverheat;
using android::hardware::google::pixel::reportUsbPortOverheat;
using android::hardware::google::pixel::usb::getI2cClientPath;
using android::String8;
using android::Vector;
@ -67,18 +69,19 @@ volatile bool destroyThread;
string enabledPath;
constexpr char kHsi2cPath[] = "/sys/devices/platform/10d60000.hsi2c";
constexpr char kI2CPath[] = "/sys/devices/platform/10d60000.hsi2c/i2c-";
constexpr char kTcpcDevName[] = "i2c-max77759tcpc";
constexpr char kI2cClientId[] = "0025";
constexpr char kComplianceWarningsPath[] = "device/non_compliant_reasons";
constexpr char kComplianceWarningBC12[] = "bc12";
constexpr char kComplianceWarningDebugAccessory[] = "debug-accessory";
constexpr char kComplianceWarningMissingRp[] = "missing_rp";
constexpr char kComplianceWarningOther[] = "other";
constexpr char kComplianceWarningInputPowerLimited[] = "input_power_limited";
constexpr char kContaminantDetectionPath[] = "i2c-max77759tcpc/contaminant_detection";
constexpr char kStatusPath[] = "i2c-max77759tcpc/contaminant_detection_status";
constexpr char kSinkLimitEnable[] = "i2c-max77759tcpc/usb_limit_sink_enable";
constexpr char kSourceLimitEnable[] = "i2c-max77759tcpc/usb_limit_source_enable";
constexpr char kSinkLimitCurrent[] = "i2c-max77759tcpc/usb_limit_sink_current";
constexpr char kContaminantDetectionPath[] = "contaminant_detection";
constexpr char kStatusPath[] = "contaminant_detection_status";
constexpr char kSinkLimitEnable[] = "usb_limit_sink_enable";
constexpr char kSourceLimitEnable[] = "usb_limit_source_enable";
constexpr char kSinkLimitCurrent[] = "usb_limit_sink_current";
constexpr char kTypecPath[] = "/sys/class/typec";
constexpr char kDisableContatminantDetection[] = "vendor.usb.contaminantdisable";
constexpr char kOverheatStatsPath[] = "/sys/devices/platform/google,usbc_port_cooling_dev/";
@ -245,31 +248,9 @@ ScopedAStatus Usb::resetUsbPort(const std::string& in_portName, int64_t in_trans
return ::ndk::ScopedAStatus::ok();
}
Status getI2cBusHelper(string *name) {
DIR *dp;
dp = opendir(kHsi2cPath);
if (dp != NULL) {
struct dirent *ep;
while ((ep = readdir(dp))) {
if (ep->d_type == DT_DIR) {
if (string::npos != string(ep->d_name).find("i2c-")) {
std::strtok(ep->d_name, "-");
*name = std::strtok(NULL, "-");
}
}
}
closedir(dp);
return Status::SUCCESS;
}
ALOGE("Failed to open %s", kHsi2cPath);
return Status::ERROR;
}
Status queryMoistureDetectionStatus(std::vector<PortStatus> *currentPortStatus) {
string enabled, status, path, DetectedPath;
Status queryMoistureDetectionStatus(android::hardware::usb::Usb *usb,
std::vector<PortStatus> *currentPortStatus) {
string enabled, status, DetectedPath;
(*currentPortStatus)[0].supportedContaminantProtectionModes
.push_back(ContaminantProtectionMode::FORCE_DISABLE);
@ -278,8 +259,15 @@ Status queryMoistureDetectionStatus(std::vector<PortStatus> *currentPortStatus)
(*currentPortStatus)[0].supportsEnableContaminantPresenceDetection = true;
(*currentPortStatus)[0].supportsEnableContaminantPresenceProtection = false;
getI2cBusHelper(&path);
enabledPath = kI2CPath + path + "/" + kContaminantDetectionPath;
if (usb->mI2cClientPath.empty()) {
usb->mI2cClientPath = getI2cClientPath(kHsi2cPath, kTcpcDevName, kI2cClientId);
if (usb->mI2cClientPath.empty()) {
ALOGE("%s: Unable to locate i2c bus node", __func__);
return Status::ERROR;
}
}
enabledPath = usb->mI2cClientPath + kContaminantDetectionPath;
if (!ReadFileToString(enabledPath, &enabled)) {
ALOGE("Failed to open moisture_detection_enabled");
return Status::ERROR;
@ -287,7 +275,7 @@ Status queryMoistureDetectionStatus(std::vector<PortStatus> *currentPortStatus)
enabled = Trim(enabled);
if (enabled == "1") {
DetectedPath = kI2CPath + path + "/" + kStatusPath;
DetectedPath = usb->mI2cClientPath + kStatusPath;
if (!ReadFileToString(DetectedPath, &status)) {
ALOGE("Failed to open moisture_detected");
return Status::ERROR;
@ -562,7 +550,8 @@ Usb::Usb()
ThrottlingSeverity::NONE)}, kSamplingIntervalSec),
mUsbDataEnabled(true),
mUsbHubVendorCmdValue(GL852G_VENDOR_CMD_VALUE_DEFAULT),
mUsbHubVendorCmdIndex(GL852G_VENDOR_CMD_INDEX_DEFAULT) {
mUsbHubVendorCmdIndex(GL852G_VENDOR_CMD_INDEX_DEFAULT),
mI2cClientPath("") {
pthread_condattr_t attr;
if (pthread_condattr_init(&attr)) {
ALOGE("pthread_condattr_init failed: %s", strerror(errno));
@ -650,12 +639,19 @@ ScopedAStatus Usb::limitPowerTransfer(const string& in_portName, bool in_limit,
int64_t in_transactionId) {
bool sessionFail = false, success;
std::vector<PortStatus> currentPortStatus;
string path, sinkLimitEnablePath, currentLimitPath, sourceLimitEnablePath;
string sinkLimitEnablePath, currentLimitPath, sourceLimitEnablePath;
getI2cBusHelper(&path);
sinkLimitEnablePath = kI2CPath + path + "/" + kSinkLimitEnable;
currentLimitPath = kI2CPath + path + "/" + kSinkLimitCurrent;
sourceLimitEnablePath = kI2CPath + path + "/" + kSourceLimitEnable;
if (mI2cClientPath.empty()) {
mI2cClientPath = getI2cClientPath(kHsi2cPath, kTcpcDevName, kI2cClientId);
if (mI2cClientPath.empty()) {
ALOGE("%s: Unable to locate i2c bus node", __func__);
return ScopedAStatus::ok();
}
}
sinkLimitEnablePath = mI2cClientPath + kSinkLimitEnable;
currentLimitPath = mI2cClientPath + kSinkLimitCurrent;
sourceLimitEnablePath = mI2cClientPath + kSourceLimitEnable;
pthread_mutex_lock(&mLock);
if (in_limit) {
@ -695,11 +691,19 @@ ScopedAStatus Usb::limitPowerTransfer(const string& in_portName, bool in_limit,
return ScopedAStatus::ok();
}
Status queryPowerTransferStatus(std::vector<PortStatus> *currentPortStatus) {
string limitedPath, enabled, path;
Status queryPowerTransferStatus(android::hardware::usb::Usb *usb,
std::vector<PortStatus> *currentPortStatus) {
string limitedPath, enabled;
getI2cBusHelper(&path);
limitedPath = kI2CPath + path + "/" + kSinkLimitEnable;
if (usb->mI2cClientPath.empty()) {
usb->mI2cClientPath = getI2cClientPath(kHsi2cPath, kTcpcDevName, kI2cClientId);
if (usb->mI2cClientPath.empty()) {
ALOGE("%s: Unable to locate i2c bus node", __func__);
return Status::ERROR;
}
}
limitedPath = usb->mI2cClientPath + kSinkLimitEnable;
if (!ReadFileToString(limitedPath, &enabled)) {
ALOGE("Failed to open limit_sink_enable");
return Status::ERROR;
@ -953,8 +957,8 @@ void queryVersionHelper(android::hardware::usb::Usb *usb,
Status status;
pthread_mutex_lock(&usb->mLock);
status = getPortStatusHelper(usb, currentPortStatus);
queryMoistureDetectionStatus(currentPortStatus);
queryPowerTransferStatus(currentPortStatus);
queryMoistureDetectionStatus(usb, currentPortStatus);
queryPowerTransferStatus(usb, currentPortStatus);
queryNonCompliantChargerStatus(currentPortStatus);
queryUsbDataSession(usb, currentPortStatus);
if (usb->mCallback != NULL) {

View file

@ -99,6 +99,7 @@ struct Usb : public BnUsb {
// Usb hub vendor command settings for JK level tuning
int mUsbHubVendorCmdValue;
int mUsbHubVendorCmdIndex;
std::string mI2cClientPath;
private:
pthread_t mPoll;