1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "libpixelusb-i2chelper"
18 
19 #include "include/pixelusb/I2cHelper.h"
20 
21 #include <dirent.h>
22 #include <utils/Log.h>
23 
24 namespace android {
25 namespace hardware {
26 namespace google {
27 namespace pixel {
28 namespace usb {
29 
30 // getI2cBusNumberString: Return the I2C bus number as the string type
31 //
32 // The bus number can be extracted from the sub-directory under the hsi2c sysfs
33 // device directory (e.g. /sys/devices/platform/10d60000.hsi2c/) and the pattern
34 // of the sub-directory is /^i2c-\d+$/ where \d+ is the bus number.
getI2cBusNumberString(const string hsi2cPath)35 static string getI2cBusNumberString(const string hsi2cPath) {
36     DIR *dp = opendir(hsi2cPath.c_str());
37 
38     if (dp != NULL) {
39         struct dirent *ep;
40 
41         while ((ep = readdir(dp))) {
42             if (ep->d_type == DT_DIR) {
43                 // Supposed that there is only one sub dir in the pattern "i2c-"
44                 if (string::npos != string(ep->d_name).find("i2c-")) {
45                     std::strtok(ep->d_name, "-");
46                     string busNumber = std::strtok(NULL, "-");
47                     closedir(dp);
48                     return busNumber;
49                 }
50             }
51         }
52         closedir(dp);
53         ALOGE("Failed to find the i2c sub dir under %s", hsi2cPath.c_str());
54         return string("");
55     }
56 
57     ALOGE("Failed to open %s", hsi2cPath.c_str());
58     return string("");
59 }
60 
61 // getI2cClientPath: Return the full path of the I2C client directory
62 //
63 // There are two forms of the directory path: in client ID and in I2C device name
64 // For example:
65 //   client ID: /sys/devices/platform/10d60000.hsi2c/i2c-7/7-0025/
66 //   device name: /sys/devices/platform/10d60000.hsi2c/i2c-7/i2c-max77759tcpc/
67 //
68 // The bus number and the client directory name differs across kernel versions and
69 // build targets. Search the bus number first to locate the first level of the sub
70 // directory, and then search the I2C device name under it.
71 //
72 // Append the I2c device name to the full path if found, otherwise, append "bus
73 // number" + "-" + client ID. Note that the client ID must be a 4-digit number
74 // with 0 stuffed in the type of string.
getI2cClientPath(const string hsi2cPath,const string devName,const string clientId)75 string getI2cClientPath(const string hsi2cPath, const string devName, const string clientId) {
76     DIR *dp;
77     string strBusNumber, i2cPathPartial, i2cClientPath;
78 
79     strBusNumber = getI2cBusNumberString(hsi2cPath);
80     if (strBusNumber.empty()) {
81         return string("");
82     }
83 
84     i2cPathPartial = hsi2cPath + "/i2c-" + strBusNumber;
85     dp = opendir(i2cPathPartial.c_str());
86     if (dp != NULL) {
87         struct dirent *ep;
88         string i2cClientDevice = strBusNumber + "-" + clientId;
89 
90         while ((ep = readdir(dp))) {
91             if (ep->d_type == DT_DIR) {
92                 if (string::npos != string(ep->d_name).find(devName)) {
93                     closedir(dp);
94                     return string(i2cPathPartial + "/" + devName + "/");
95                 }
96                 if (string::npos != string(ep->d_name).find(i2cClientDevice)) {
97                     closedir(dp);
98                     return string(i2cPathPartial + "/" + i2cClientDevice + "/");
99                 }
100             }
101         }
102         closedir(dp);
103     }
104 
105     ALOGE("Failed to open %s", i2cPathPartial.c_str());
106     return string("");
107 }
108 
109 }  // namespace usb
110 }  // namespace pixel
111 }  // namespace google
112 }  // namespace hardware
113 }  // namespace android
114