usb: Update Usb and UsbGadget i2c logic

Update the logic to find the i2c bus number to use either the named i2c
devices or the static i2c bus numbers. This allows us to support both
cases -- v5.10 uses i2c-<devname> while v6.1 uses <i2c-bus#>-<reg>.

Bug: 291606723
Test: verify no errors from the usb services with v5.10 and v6.1
Change-Id: I6d41ac041ead68e72a3766e03d491bcd478468d3
This commit is contained in:
Will McVicker 2023-09-20 16:15:13 -07:00 committed by Treehugger Robot
parent b1662cade2
commit b54d8f91e3
4 changed files with 220 additions and 84 deletions

View file

@ -59,17 +59,18 @@ volatile bool destroyThread;
string enabledPath;
constexpr char kHsi2cPath[] = "/sys/devices/platform/10d50000.hsi2c";
constexpr char kI2CPath[] = "/sys/devices/platform/10d50000.hsi2c/i2c-";
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 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 kMax77759TcpcDevName[] = "i2c-max77759tcpc";
constexpr unsigned int kMax77759TcpcClientId = 0x25;
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/";
@ -220,8 +221,15 @@ ScopedAStatus Usb::resetUsbPort(const std::string& in_portName, int64_t in_trans
return ::ndk::ScopedAStatus::ok();
}
Status getI2cBusHelper(string *name) {
int Usb::getI2cBusNumber() {
DIR *dp;
unsigned int busNumber;
// Since the i2c bus number doesn't change after boot, we only need to get
// it once.
if (mI2cBusNumber >= 0) {
return mI2cBusNumber;
}
dp = opendir(kHsi2cPath);
if (dp != NULL) {
@ -229,22 +237,65 @@ Status getI2cBusHelper(string *name) {
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, "-");
if (sscanf(ep->d_name, "i2c-%u", &busNumber) == 1) {
mI2cBusNumber = busNumber;
break;
}
}
}
closedir(dp);
return Status::SUCCESS;
}
ALOGE("Failed to open %s", kHsi2cPath);
return Status::ERROR;
if (mI2cBusNumber < 0) {
ALOGE("Failed to open %s", kHsi2cPath);
}
return mI2cBusNumber;
}
Status queryMoistureDetectionStatus(std::vector<PortStatus> *currentPortStatus) {
string enabled, status, path, DetectedPath;
std::string_view Usb::getI2cClientPath() {
DIR *dp;
char i2cClientPathLabeled[PATH_MAX];
char i2cClientPathUnLabeled[PATH_MAX];
// Since the I2C client path doesn't change after boot, we only need to get
// it once.
if (!mI2cClientPath.empty()) {
return mI2cClientPath;
}
if (getI2cBusNumber() < 0) {
return std::string_view{""};
}
snprintf(i2cClientPathLabeled, sizeof(i2cClientPathLabeled),
"%s/i2c-%d/%s", kHsi2cPath, mI2cBusNumber, kMax77759TcpcDevName);
snprintf(i2cClientPathUnLabeled, sizeof(i2cClientPathUnLabeled),
"%s/i2c-%d/%d-%04x", kHsi2cPath, mI2cBusNumber, mI2cBusNumber,
kMax77759TcpcClientId);
dp = opendir(i2cClientPathLabeled);
if (dp != NULL) {
mI2cClientPath.assign(i2cClientPathLabeled);
closedir(dp);
return mI2cClientPath;
}
dp = opendir(i2cClientPathUnLabeled);
if (dp != NULL) {
mI2cClientPath.assign(i2cClientPathUnLabeled);
closedir(dp);
return mI2cClientPath;
}
ALOGE("Failed to find the i2c client path under %s", kHsi2cPath);
return std::string_view{""};
}
Status queryMoistureDetectionStatus(android::hardware::usb::Usb *usb,
std::vector<PortStatus> *currentPortStatus)
{
string enabled, status, DetectedPath;
std::string_view i2cPath;
(*currentPortStatus)[0].supportedContaminantProtectionModes
.push_back(ContaminantProtectionMode::FORCE_DISABLE);
@ -253,8 +304,12 @@ Status queryMoistureDetectionStatus(std::vector<PortStatus> *currentPortStatus)
(*currentPortStatus)[0].supportsEnableContaminantPresenceDetection = true;
(*currentPortStatus)[0].supportsEnableContaminantPresenceProtection = false;
getI2cBusHelper(&path);
enabledPath = kI2CPath + path + "/" + kContaminantDetectionPath;
i2cPath = usb->getI2cClientPath();
if (i2cPath.empty()) {
ALOGE("%s: Unable to locate i2c bus node", __func__);
return Status::ERROR;
}
enabledPath = std::string{i2cPath} + "/" + kContaminantDetectionPath;
if (!ReadFileToString(enabledPath, &enabled)) {
ALOGE("Failed to open moisture_detection_enabled");
return Status::ERROR;
@ -262,7 +317,7 @@ Status queryMoistureDetectionStatus(std::vector<PortStatus> *currentPortStatus)
enabled = Trim(enabled);
if (enabled == "1") {
DetectedPath = kI2CPath + path + "/" + kStatusPath;
DetectedPath = std::string{i2cPath} + "/" + kStatusPath;
if (!ReadFileToString(DetectedPath, &status)) {
ALOGE("Failed to open moisture_detected");
return Status::ERROR;
@ -460,7 +515,9 @@ Usb::Usb()
ThrottlingSeverity::NONE),
ZoneInfo(TemperatureType::UNKNOWN, kThermalZoneForTempReadSecondary2,
ThrottlingSeverity::NONE)}, kSamplingIntervalSec),
mUsbDataEnabled(true) {
mUsbDataEnabled(true),
mI2cBusNumber(-1),
mI2cClientPath("") {
pthread_condattr_t attr;
if (pthread_condattr_init(&attr)) {
ALOGE("pthread_condattr_init failed: %s", strerror(errno));
@ -539,32 +596,38 @@ 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;
getI2cBusHelper(&path);
sinkLimitEnablePath = kI2CPath + path + "/" + kSinkLimitEnable;
sourceLimitEnablePath = kI2CPath + path + "/" + kSourceLimitEnable;
currentLimitPath = kI2CPath + path + "/" + kSinkLimitCurrent;
string sinkLimitEnablePath, currentLimitPath, sourceLimitEnablePath;
std::string_view i2cPath;
pthread_mutex_lock(&mLock);
if (in_limit) {
success = WriteStringToFile("0", currentLimitPath);
i2cPath = getI2cClientPath();
if (!i2cPath.empty()) {
sinkLimitEnablePath = std::string{i2cPath} + "/" + kSinkLimitEnable;
sourceLimitEnablePath = std::string{i2cPath} + "/" + kSourceLimitEnable;
currentLimitPath = std::string{i2cPath} + "/" + kSinkLimitCurrent;
if (in_limit) {
success = WriteStringToFile("0", currentLimitPath);
if (!success) {
ALOGE("Failed to set sink current limit");
sessionFail = true;
}
}
success = WriteStringToFile(in_limit ? "1" : "0", sinkLimitEnablePath);
if (!success) {
ALOGE("Failed to set sink current limit");
ALOGE("Failed to %s sink current limit: %s", in_limit ? "enable" : "disable",
sinkLimitEnablePath.c_str());
sessionFail = true;
}
}
success = WriteStringToFile(in_limit ? "1" : "0", sinkLimitEnablePath);
if (!success) {
ALOGE("Failed to %s sink current limit: %s", in_limit ? "enable" : "disable",
sinkLimitEnablePath.c_str());
success = WriteStringToFile(in_limit ? "1" : "0", sourceLimitEnablePath);
if (!success) {
ALOGE("Failed to %s source current limit: %s", in_limit ? "enable" : "disable",
sourceLimitEnablePath.c_str());
sessionFail = true;
}
} else {
sessionFail = true;
}
success = WriteStringToFile(in_limit ? "1" : "0", sourceLimitEnablePath);
if (!success) {
ALOGE("Failed to %s source current limit: %s", in_limit ? "enable" : "disable",
sourceLimitEnablePath.c_str());
sessionFail = true;
ALOGE("%s: Unable to locate i2c bus node", __func__);
}
ALOGI("limitPowerTransfer limit:%c opId:%ld", in_limit ? 'y' : 'n', in_transactionId);
if (mCallback != NULL && in_transactionId >= 0) {
@ -583,11 +646,17 @@ 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;
std::string_view i2cPath;
getI2cBusHelper(&path);
limitedPath = kI2CPath + path + "/" + kSinkLimitEnable;
i2cPath = usb->getI2cClientPath();
if (i2cPath.empty()) {
ALOGE("%s: Unable to locate i2c bus node", __func__);
return Status::ERROR;
}
limitedPath = std::string{i2cPath} + "/" + kSinkLimitEnable;
if (!ReadFileToString(limitedPath, &enabled)) {
ALOGE("Failed to open limit_sink_enable");
return Status::ERROR;
@ -823,8 +892,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);
if (usb->mCallback != NULL) {
ScopedAStatus ret = usb->mCallback->notifyPortStatusChange(*currentPortStatus,

View file

@ -111,9 +111,13 @@ struct Usb : public BnUsb {
std::function<void(uint32_t)> cb;
};
std::map<std::string, struct epollEntry> mEpollEntries;
int getI2cBusNumber();
std::string_view getI2cClientPath();
private:
pthread_t mPoll;
int mI2cBusNumber;
std::string mI2cClientPath;
};
} // namespace usb