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-usbbushelper"
18
19 #include "include/pixelusb/UsbBusHelper.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
getBusNumberString(const string busType,const string busPath)30 static string getBusNumberString(const string busType, const string busPath) {
31 DIR *dp = opendir(busPath.c_str());
32
33 if (dp != NULL) {
34 struct dirent *ep;
35
36 while ((ep = readdir(dp))) {
37 if (ep->d_type == DT_DIR) {
38 // Supposed that there is only one sub dir in the busType pattern
39 if (string::npos != string(ep->d_name).find(string(busType + "-"))) {
40 std::strtok(ep->d_name, "-");
41 string busNumber = std::strtok(NULL, "-");
42 closedir(dp);
43 return busNumber;
44 }
45 }
46 }
47 closedir(dp);
48 ALOGE("Failed to find the %s sub dir under %s", busType.c_str(), busPath.c_str());
49 return string("");
50 }
51
52 ALOGE("Failed to open %s", busPath.c_str());
53 return string("");
54 }
55
56 /*
57 * getBusClientPath: Return the full path of the USB Bus client directory
58 *
59 * The two bus interfaces being used are I2c and SPMI. They can be returned
60 * in the following formats:
61 * I2c:
62 * client ID: /sys/devices/platform/10d60000.hsi2c/i2c-7/7-0025/
63 * device name: /sys/devices/platform/10d60000.hsi2c/i2c-7/i2c-max77759tcpc/
64 * SPMI:
65 * client ID: /sys/devices/platform/53f1000.spmi/spmi-0/0-04/
66 *
67 * For I2c, the bus number and client directory name differs across kernel
68 * versions and build targets. Search the bus number first to locate
69 * the first level of the sub directory, and then search the I2c device name
70 * under it.
71 *
72 * Append the I2c device name to the full path if found. Otherwise for I2c and
73 * SPMI, append: busNumber + "-" + clientId. clientId is a 4-digit number with
74 * 0 stuffed in the type of string for I2c, or a 2-digit number for SPMI.
75 *
76 */
getBusClientPath(const string busType,const string busPath,const string devName,const string clientId)77 string getBusClientPath(const string busType, const string busPath, const string devName,
78 const string clientId) {
79 DIR *dp;
80 string strBusNumber, busPathPartial, busClientPath;
81
82 strBusNumber = getBusNumberString(busType, busPath);
83 if (strBusNumber.empty()) {
84 return string("");
85 }
86
87 busPathPartial = busPath + "/" + busType + "-" + strBusNumber;
88 dp = opendir(busPathPartial.c_str());
89 if (dp != NULL) {
90 struct dirent *ep;
91 string busClientDevice = strBusNumber + "-" + clientId;
92
93 while ((ep = readdir(dp))) {
94 if (ep->d_type == DT_DIR) {
95 if (!devName.empty() && string::npos != string(ep->d_name).find(devName)) {
96 closedir(dp);
97 return string(busPathPartial + "/" + devName + "/");
98 }
99 if (string::npos != string(ep->d_name).find(busClientDevice)) {
100 closedir(dp);
101 return string(busPathPartial + "/" + busClientDevice + "/");
102 }
103 }
104 }
105 closedir(dp);
106 }
107
108 ALOGE("Failed to open %s", busPathPartial.c_str());
109 return string("");
110 }
111
112 } // namespace usb
113 } // namespace pixel
114 } // namespace google
115 } // namespace hardware
116 } // namespace android
117