Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 17 | #define LOG_TAG "android.hardware.usb.gadget.aidl-service" |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 18 | |
| 19 | #include "UsbGadget.h" |
| 20 | #include <dirent.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <stdio.h> |
| 23 | #include <sys/inotify.h> |
| 24 | #include <sys/mount.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <unistd.h> |
| 28 | |
Avichal Rakesh | eafdae6 | 2023-01-06 02:47:19 -0800 | [diff] [blame^] | 29 | #include <android-base/properties.h> |
| 30 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 31 | #include <aidl/android/frameworks/stats/IStats.h> |
| 32 | |
| 33 | namespace aidl { |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 34 | namespace android { |
| 35 | namespace hardware { |
| 36 | namespace usb { |
| 37 | namespace gadget { |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 38 | |
Ricky Niu | ebd7fca | 2022-05-09 10:14:20 +0800 | [diff] [blame] | 39 | string enabledPath; |
| 40 | constexpr char kHsi2cPath[] = "/sys/devices/platform/10d50000.hsi2c"; |
| 41 | constexpr char kI2CPath[] = "/sys/devices/platform/10d50000.hsi2c/i2c-"; |
| 42 | constexpr char kAccessoryLimitCurrent[] = "i2c-max77759tcpc/usb_limit_accessory_current"; |
| 43 | constexpr char kAccessoryLimitCurrentEnable[] = "i2c-max77759tcpc/usb_limit_accessory_enable"; |
| 44 | |
Avichal Rakesh | eafdae6 | 2023-01-06 02:47:19 -0800 | [diff] [blame^] | 45 | using ::android::base::GetBoolProperty; |
| 46 | using ::android::hardware::google::pixel::usb::kUvcEnabled; |
| 47 | |
Ray Chi | ba3dc9b | 2022-03-01 21:57:03 +0800 | [diff] [blame] | 48 | UsbGadget::UsbGadget() : mGadgetIrqPath("") { |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 49 | if (access(OS_DESC_PATH, R_OK) != 0) { |
| 50 | ALOGE("configfs setup not done yet"); |
| 51 | abort(); |
| 52 | } |
| 53 | } |
| 54 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 55 | Status UsbGadget::getUsbGadgetIrqPath() { |
Ray Chi | ba3dc9b | 2022-03-01 21:57:03 +0800 | [diff] [blame] | 56 | std::string irqs; |
| 57 | size_t read_pos = 0; |
| 58 | size_t found_pos = 0; |
| 59 | |
| 60 | if (!ReadFileToString(kProcInterruptsPath, &irqs)) { |
| 61 | ALOGE("cannot read all interrupts"); |
| 62 | return Status::ERROR; |
| 63 | } |
| 64 | |
| 65 | while (true) { |
| 66 | found_pos = irqs.find_first_of("\n", read_pos); |
| 67 | if (found_pos == std::string::npos) { |
| 68 | ALOGI("the string of all interrupts is unexpected"); |
| 69 | return Status::ERROR; |
| 70 | } |
| 71 | |
| 72 | std::string single_irq = irqs.substr(read_pos, found_pos - read_pos); |
| 73 | |
| 74 | if (single_irq.find("dwc3", 0) != std::string::npos) { |
| 75 | unsigned int dwc3_irq_number; |
| 76 | size_t dwc3_pos = single_irq.find_first_of(":"); |
| 77 | if (!ParseUint(single_irq.substr(0, dwc3_pos), &dwc3_irq_number)) { |
| 78 | ALOGI("unknown IRQ strings"); |
| 79 | return Status::ERROR; |
| 80 | } |
| 81 | |
| 82 | mGadgetIrqPath = kProcIrqPath + single_irq.substr(0, dwc3_pos) + kSmpAffinityList; |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | if (found_pos == irqs.npos) { |
| 87 | ALOGI("USB gadget doesn't start"); |
| 88 | return Status::ERROR; |
| 89 | } |
| 90 | |
| 91 | read_pos = found_pos + 1; |
| 92 | } |
| 93 | |
| 94 | return Status::SUCCESS; |
| 95 | } |
| 96 | |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 97 | void currentFunctionsAppliedCallback(bool functionsApplied, void *payload) { |
| 98 | UsbGadget *gadget = (UsbGadget *)payload; |
| 99 | gadget->mCurrentUsbFunctionsApplied = functionsApplied; |
| 100 | } |
| 101 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 102 | ScopedAStatus UsbGadget::getCurrentUsbFunctions(const shared_ptr<IUsbGadgetCallback> &callback, |
| 103 | int64_t in_transactionId) { |
| 104 | ScopedAStatus ret = callback->getCurrentUsbFunctionsCb( |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 105 | mCurrentUsbFunctions, |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 106 | mCurrentUsbFunctionsApplied ? Status::FUNCTIONS_APPLIED : Status::FUNCTIONS_NOT_APPLIED, |
| 107 | in_transactionId); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 108 | if (!ret.isOk()) |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 109 | ALOGE("Call to getCurrentUsbFunctionsCb failed %s", ret.getDescription().c_str()); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 110 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 111 | return ScopedAStatus::ok(); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 112 | } |
| 113 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 114 | ScopedAStatus UsbGadget::getUsbSpeed(const shared_ptr<IUsbGadgetCallback> &callback, |
| 115 | int64_t in_transactionId) { |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 116 | std::string current_speed; |
| 117 | if (ReadFileToString(SPEED_PATH, ¤t_speed)) { |
| 118 | current_speed = Trim(current_speed); |
| 119 | ALOGI("current USB speed is %s", current_speed.c_str()); |
| 120 | if (current_speed == "low-speed") |
| 121 | mUsbSpeed = UsbSpeed::LOWSPEED; |
| 122 | else if (current_speed == "full-speed") |
| 123 | mUsbSpeed = UsbSpeed::FULLSPEED; |
| 124 | else if (current_speed == "high-speed") |
| 125 | mUsbSpeed = UsbSpeed::HIGHSPEED; |
| 126 | else if (current_speed == "super-speed") |
| 127 | mUsbSpeed = UsbSpeed::SUPERSPEED; |
| 128 | else if (current_speed == "super-speed-plus") |
| 129 | mUsbSpeed = UsbSpeed::SUPERSPEED_10Gb; |
| 130 | else if (current_speed == "UNKNOWN") |
| 131 | mUsbSpeed = UsbSpeed::UNKNOWN; |
| 132 | else |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 133 | mUsbSpeed = UsbSpeed::UNKNOWN; |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 134 | } else { |
| 135 | ALOGE("Fail to read current speed"); |
| 136 | mUsbSpeed = UsbSpeed::UNKNOWN; |
| 137 | } |
| 138 | |
| 139 | if (callback) { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 140 | ScopedAStatus ret = callback->getUsbSpeedCb(mUsbSpeed, in_transactionId); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 141 | |
| 142 | if (!ret.isOk()) |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 143 | ALOGE("Call to getUsbSpeedCb failed %s", ret.getDescription().c_str()); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 144 | } |
| 145 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 146 | return ScopedAStatus::ok(); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 147 | } |
| 148 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 149 | Status UsbGadget::tearDownGadget() { |
| 150 | if (Status(resetGadget()) != Status::SUCCESS){ |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 151 | return Status::ERROR; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 152 | } |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 153 | |
| 154 | if (monitorFfs.isMonitorRunning()) { |
| 155 | monitorFfs.reset(); |
| 156 | } else { |
| 157 | ALOGI("mMonitor not running"); |
| 158 | } |
| 159 | return Status::SUCCESS; |
| 160 | } |
| 161 | |
Avichal Rakesh | eafdae6 | 2023-01-06 02:47:19 -0800 | [diff] [blame^] | 162 | static Status validateAndSetVidPid(int64_t functions) { |
| 163 | Status ret; |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 164 | std::string vendorFunctions = getVendorFunctions(); |
| 165 | |
| 166 | switch (functions) { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 167 | case GadgetFunction::MTP: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 168 | if (!(vendorFunctions == "user" || vendorFunctions == "")) { |
| 169 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
| 170 | ret = Status::CONFIGURATION_NOT_SUPPORTED; |
| 171 | } else { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 172 | ret = Status(setVidPid("0x18d1", "0x4ee1")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 173 | } |
| 174 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 175 | case GadgetFunction::ADB | |
| 176 | GadgetFunction::MTP: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 177 | if (!(vendorFunctions == "user" || vendorFunctions == "")) { |
| 178 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
| 179 | ret = Status::CONFIGURATION_NOT_SUPPORTED; |
| 180 | } else { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 181 | ret = Status(setVidPid("0x18d1", "0x4ee2")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 182 | } |
| 183 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 184 | case GadgetFunction::RNDIS: |
| 185 | case GadgetFunction::RNDIS | |
| 186 | GadgetFunction::NCM: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 187 | if (!(vendorFunctions == "user" || vendorFunctions == "")) { |
| 188 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
| 189 | ret = Status::CONFIGURATION_NOT_SUPPORTED; |
| 190 | } else { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 191 | ret = Status(setVidPid("0x18d1", "0x4ee3")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 192 | } |
| 193 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 194 | case GadgetFunction::ADB | |
| 195 | GadgetFunction::RNDIS: |
| 196 | case GadgetFunction::ADB | |
| 197 | GadgetFunction::RNDIS | |
| 198 | GadgetFunction::NCM: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 199 | if (vendorFunctions == "dm") { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 200 | ret = Status(setVidPid("0x04e8", "0x6862")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 201 | } else { |
| 202 | if (!(vendorFunctions == "user" || vendorFunctions == "")) { |
| 203 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
| 204 | ret = Status::CONFIGURATION_NOT_SUPPORTED; |
| 205 | } else { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 206 | ret = Status(setVidPid("0x18d1", "0x4ee4")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 210 | case GadgetFunction::PTP: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 211 | if (!(vendorFunctions == "user" || vendorFunctions == "")) { |
| 212 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
| 213 | ret = Status::CONFIGURATION_NOT_SUPPORTED; |
| 214 | } else { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 215 | ret = Status(setVidPid("0x18d1", "0x4ee5")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 216 | } |
| 217 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 218 | case GadgetFunction::ADB | |
| 219 | GadgetFunction::PTP: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 220 | if (!(vendorFunctions == "user" || vendorFunctions == "")) { |
| 221 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
| 222 | ret = Status::CONFIGURATION_NOT_SUPPORTED; |
| 223 | } else { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 224 | ret = Status(setVidPid("0x18d1", "0x4ee6")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 225 | } |
| 226 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 227 | case GadgetFunction::ADB: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 228 | if (vendorFunctions == "dm") { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 229 | ret = Status(setVidPid("0x04e8", "0x6862")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 230 | } else if (vendorFunctions == "etr_miu") { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 231 | ret = Status(setVidPid("0x18d1", "0x4ee2")); |
Puma Hsu | fbcb7ad | 2021-08-19 19:36:51 +0800 | [diff] [blame] | 232 | } else if (vendorFunctions == "uwb_acm"){ |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 233 | ret = Status(setVidPid("0x18d1", "0x4ee2")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 234 | } else { |
| 235 | if (!(vendorFunctions == "user" || vendorFunctions == "")) { |
| 236 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
| 237 | ret = Status::CONFIGURATION_NOT_SUPPORTED; |
| 238 | } else { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 239 | ret = Status(setVidPid("0x18d1", "0x4ee7")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 243 | case GadgetFunction::MIDI: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 244 | if (!(vendorFunctions == "user" || vendorFunctions == "")) { |
| 245 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
| 246 | ret = Status::CONFIGURATION_NOT_SUPPORTED; |
| 247 | } else { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 248 | ret = Status(setVidPid("0x18d1", "0x4ee8")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 249 | } |
| 250 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 251 | case GadgetFunction::ADB | |
| 252 | GadgetFunction::MIDI: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 253 | if (!(vendorFunctions == "user" || vendorFunctions == "")) { |
| 254 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
| 255 | ret = Status::CONFIGURATION_NOT_SUPPORTED; |
| 256 | } else { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 257 | ret = Status(setVidPid("0x18d1", "0x4ee9")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 258 | } |
| 259 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 260 | case GadgetFunction::ACCESSORY: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 261 | if (!(vendorFunctions == "user" || vendorFunctions == "")) |
| 262 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 263 | ret = Status(setVidPid("0x18d1", "0x2d00")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 264 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 265 | case GadgetFunction::ADB | |
| 266 | GadgetFunction::ACCESSORY: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 267 | if (!(vendorFunctions == "user" || vendorFunctions == "")) |
| 268 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 269 | ret = Status(setVidPid("0x18d1", "0x2d01")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 270 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 271 | case GadgetFunction::AUDIO_SOURCE: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 272 | if (!(vendorFunctions == "user" || vendorFunctions == "")) |
| 273 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 274 | ret = Status(setVidPid("0x18d1", "0x2d02")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 275 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 276 | case GadgetFunction::ADB | |
| 277 | GadgetFunction::AUDIO_SOURCE: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 278 | if (!(vendorFunctions == "user" || vendorFunctions == "")) |
| 279 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 280 | ret = Status(setVidPid("0x18d1", "0x2d03")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 281 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 282 | case GadgetFunction::ACCESSORY | |
| 283 | GadgetFunction::AUDIO_SOURCE: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 284 | if (!(vendorFunctions == "user" || vendorFunctions == "")) |
| 285 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 286 | ret = Status(setVidPid("0x18d1", "0x2d04")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 287 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 288 | case GadgetFunction::ADB | |
| 289 | GadgetFunction::ACCESSORY | |
| 290 | GadgetFunction::AUDIO_SOURCE: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 291 | if (!(vendorFunctions == "user" || vendorFunctions == "")) |
| 292 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 293 | ret = Status(setVidPid("0x18d1", "0x2d05")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 294 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 295 | case GadgetFunction::NCM: |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 296 | if (!(vendorFunctions == "user" || vendorFunctions == "")) |
| 297 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 298 | ret = Status(setVidPid("0x18d1", "0x4eeb")); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 299 | break; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 300 | case GadgetFunction::ADB | |
| 301 | GadgetFunction::NCM: |
Ricky Niu | 48b3e5d | 2021-11-24 17:26:38 +0800 | [diff] [blame] | 302 | if (vendorFunctions == "dm") { |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 303 | ret = Status(setVidPid("0x04e8", "0x6862")); |
Ricky Niu | 48b3e5d | 2021-11-24 17:26:38 +0800 | [diff] [blame] | 304 | } else { |
| 305 | if (!(vendorFunctions == "user" || vendorFunctions == "")) |
| 306 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 307 | ret = Status(setVidPid("0x18d1", "0x4eec")); |
Ricky Niu | 48b3e5d | 2021-11-24 17:26:38 +0800 | [diff] [blame] | 308 | } |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 309 | break; |
Avichal Rakesh | eafdae6 | 2023-01-06 02:47:19 -0800 | [diff] [blame^] | 310 | case GadgetFunction::UVC: |
| 311 | if (!(vendorFunctions == "user" || vendorFunctions == "")) { |
| 312 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
| 313 | ret = Status::CONFIGURATION_NOT_SUPPORTED; |
| 314 | } else if (!GetBoolProperty(kUvcEnabled, false)) { |
| 315 | ALOGE("UVC function not enabled by config"); |
| 316 | ret = Status::CONFIGURATION_NOT_SUPPORTED; |
| 317 | } else { |
| 318 | ret = Status(setVidPid("0x18d1", "0x4eed")); |
| 319 | } |
| 320 | break; |
| 321 | case GadgetFunction::ADB | GadgetFunction::UVC: |
| 322 | if (!(vendorFunctions == "user" || vendorFunctions == "")) { |
| 323 | ALOGE("Invalid vendorFunctions set: %s", vendorFunctions.c_str()); |
| 324 | ret = Status::CONFIGURATION_NOT_SUPPORTED; |
| 325 | } else if (!GetBoolProperty(kUvcEnabled, false)) { |
| 326 | ALOGE("UVC function not enabled by config"); |
| 327 | ret = Status::CONFIGURATION_NOT_SUPPORTED; |
| 328 | } else { |
| 329 | ret = Status(setVidPid("0x18d1", "0x4eee")); |
| 330 | } |
| 331 | break; |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 332 | default: |
| 333 | ALOGE("Combination not supported"); |
| 334 | ret = Status::CONFIGURATION_NOT_SUPPORTED; |
| 335 | } |
| 336 | return ret; |
| 337 | } |
| 338 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 339 | ScopedAStatus UsbGadget::reset() { |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 340 | ALOGI("USB Gadget reset"); |
| 341 | |
| 342 | if (!WriteStringToFile("none", PULLUP_PATH)) { |
| 343 | ALOGI("Gadget cannot be pulled down"); |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 344 | return ScopedAStatus::fromServiceSpecificErrorWithMessage( |
| 345 | -1, "Gadget cannot be pulled down"); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | usleep(kDisconnectWaitUs); |
| 349 | |
| 350 | if (!WriteStringToFile(kGadgetName, PULLUP_PATH)) { |
| 351 | ALOGI("Gadget cannot be pulled up"); |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 352 | return ScopedAStatus::fromServiceSpecificErrorWithMessage( |
| 353 | -1, "Gadget cannot be pulled up"); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 354 | } |
| 355 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 356 | return ScopedAStatus::ok(); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 357 | } |
| 358 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 359 | Status UsbGadget::setupFunctions(long functions, |
| 360 | const shared_ptr<IUsbGadgetCallback> &callback, uint64_t timeout, |
| 361 | int64_t in_transactionId) { |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 362 | bool ffsEnabled = false; |
| 363 | int i = 0; |
| 364 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 365 | if (Status(addGenericAndroidFunctions(&monitorFfs, functions, &ffsEnabled, &i)) != |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 366 | Status::SUCCESS) |
| 367 | return Status::ERROR; |
| 368 | |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 369 | std::string vendorFunctions = getVendorFunctions(); |
| 370 | |
| 371 | if (vendorFunctions == "dm") { |
| 372 | ALOGI("enable usbradio debug functions"); |
Maciej Żenczykowski | e0ccd8f | 2020-12-27 19:58:32 -0800 | [diff] [blame] | 373 | if ((functions & GadgetFunction::RNDIS) != 0) { |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 374 | if (linkFunction("acm.gs6", i++)) |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 375 | return Status::ERROR; |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 376 | if (linkFunction("dm.gs7", i++)) |
| 377 | return Status::ERROR; |
| 378 | } else { |
| 379 | if (linkFunction("dm.gs7", i++)) |
| 380 | return Status::ERROR; |
| 381 | if (linkFunction("acm.gs6", i++)) |
| 382 | return Status::ERROR; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 383 | } |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 384 | } else if (vendorFunctions == "etr_miu") { |
| 385 | ALOGI("enable etr_miu functions"); |
| 386 | if (linkFunction("etr_miu.gs11", i++)) |
| 387 | return Status::ERROR; |
Puma Hsu | fbcb7ad | 2021-08-19 19:36:51 +0800 | [diff] [blame] | 388 | } else if (vendorFunctions == "uwb_acm") { |
| 389 | ALOGI("enable uwb acm function"); |
| 390 | if (linkFunction("acm.uwb0", i++)) |
| 391 | return Status::ERROR; |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 392 | } |
| 393 | |
Maciej Żenczykowski | e0ccd8f | 2020-12-27 19:58:32 -0800 | [diff] [blame] | 394 | if ((functions & GadgetFunction::ADB) != 0) { |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 395 | ffsEnabled = true; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 396 | if (Status(addAdb(&monitorFfs, &i)) != Status::SUCCESS) |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 397 | return Status::ERROR; |
| 398 | } |
| 399 | |
Maciej Żenczykowski | e0ccd8f | 2020-12-27 19:58:32 -0800 | [diff] [blame] | 400 | if ((functions & GadgetFunction::NCM) != 0) { |
| 401 | ALOGI("setCurrentUsbFunctions ncm"); |
| 402 | if (linkFunction("ncm.gs9", i++)) |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 403 | return Status::ERROR; |
| 404 | } |
| 405 | |
| 406 | // Pull up the gadget right away when there are no ffs functions. |
| 407 | if (!ffsEnabled) { |
| 408 | if (!WriteStringToFile(kGadgetName, PULLUP_PATH)) |
| 409 | return Status::ERROR; |
| 410 | mCurrentUsbFunctionsApplied = true; |
| 411 | if (callback) |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 412 | callback->setCurrentUsbFunctionsCb(functions, Status::SUCCESS, in_transactionId); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 413 | return Status::SUCCESS; |
| 414 | } |
| 415 | |
| 416 | monitorFfs.registerFunctionsAppliedCallback(¤tFunctionsAppliedCallback, this); |
| 417 | // Monitors the ffs paths to pull up the gadget when descriptors are written. |
| 418 | // Also takes of the pulling up the gadget again if the userspace process |
| 419 | // dies and restarts. |
| 420 | monitorFfs.startMonitor(); |
| 421 | |
| 422 | if (kDebug) |
| 423 | ALOGI("Mainthread in Cv"); |
| 424 | |
| 425 | if (callback) { |
| 426 | bool pullup = monitorFfs.waitForPullUp(timeout); |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 427 | ScopedAStatus ret = callback->setCurrentUsbFunctionsCb( |
| 428 | functions, pullup ? Status::SUCCESS : Status::ERROR, in_transactionId); |
| 429 | if (!ret.isOk()) { |
| 430 | ALOGE("setCurrentUsbFunctionsCb error %s", ret.getDescription().c_str()); |
| 431 | return Status::ERROR; |
| 432 | } |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 433 | } |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 434 | return Status::SUCCESS; |
| 435 | } |
| 436 | |
Ricky Niu | ebd7fca | 2022-05-09 10:14:20 +0800 | [diff] [blame] | 437 | Status getI2cBusHelper(string *name) { |
| 438 | DIR *dp; |
| 439 | |
| 440 | dp = opendir(kHsi2cPath); |
| 441 | if (dp != NULL) { |
| 442 | struct dirent *ep; |
| 443 | |
| 444 | while ((ep = readdir(dp))) { |
| 445 | if (ep->d_type == DT_DIR) { |
| 446 | if (string::npos != string(ep->d_name).find("i2c-")) { |
| 447 | std::strtok(ep->d_name, "-"); |
| 448 | *name = std::strtok(NULL, "-"); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | closedir(dp); |
| 453 | return Status::SUCCESS; |
| 454 | } |
| 455 | |
| 456 | ALOGE("Failed to open %s", kHsi2cPath); |
| 457 | return Status::ERROR; |
| 458 | } |
| 459 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 460 | ScopedAStatus UsbGadget::setCurrentUsbFunctions(long functions, |
| 461 | const shared_ptr<IUsbGadgetCallback> &callback, |
| 462 | int64_t timeout, |
| 463 | int64_t in_transactionId) { |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 464 | std::unique_lock<std::mutex> lk(mLockSetCurrentFunction); |
Ricky Niu | ebd7fca | 2022-05-09 10:14:20 +0800 | [diff] [blame] | 465 | std::string current_usb_power_operation_mode, current_usb_type; |
| 466 | std::string usb_limit_sink_enable; |
| 467 | |
| 468 | string accessoryCurrentLimitEnablePath, accessoryCurrentLimitPath, path; |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 469 | |
| 470 | mCurrentUsbFunctions = functions; |
| 471 | mCurrentUsbFunctionsApplied = false; |
| 472 | |
Ricky Niu | ebd7fca | 2022-05-09 10:14:20 +0800 | [diff] [blame] | 473 | getI2cBusHelper(&path); |
| 474 | accessoryCurrentLimitPath = kI2CPath + path + "/" + kAccessoryLimitCurrent; |
| 475 | accessoryCurrentLimitEnablePath = kI2CPath + path + "/" + kAccessoryLimitCurrentEnable; |
| 476 | |
Ray Chi | ba3dc9b | 2022-03-01 21:57:03 +0800 | [diff] [blame] | 477 | // Get the gadget IRQ number before tearDownGadget() |
| 478 | if (mGadgetIrqPath.empty()) |
| 479 | getUsbGadgetIrqPath(); |
| 480 | |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 481 | // Unlink the gadget and stop the monitor if running. |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 482 | Status status = tearDownGadget(); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 483 | if (status != Status::SUCCESS) { |
| 484 | goto error; |
| 485 | } |
| 486 | |
| 487 | ALOGI("Returned from tearDown gadget"); |
| 488 | |
| 489 | // Leave the gadget pulled down to give time for the host to sense disconnect. |
| 490 | usleep(kDisconnectWaitUs); |
| 491 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 492 | if (functions == GadgetFunction::NONE) { |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 493 | if (callback == NULL) |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 494 | return ScopedAStatus::fromServiceSpecificErrorWithMessage( |
| 495 | -1, "callback == NULL"); |
| 496 | ScopedAStatus ret = callback->setCurrentUsbFunctionsCb(functions, Status::SUCCESS, in_transactionId); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 497 | if (!ret.isOk()) |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 498 | ALOGE("Error while calling setCurrentUsbFunctionsCb %s", ret.getDescription().c_str()); |
| 499 | return ScopedAStatus::fromServiceSpecificErrorWithMessage( |
| 500 | -1, "Error while calling setCurrentUsbFunctionsCb"); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | status = validateAndSetVidPid(functions); |
| 504 | |
| 505 | if (status != Status::SUCCESS) { |
| 506 | goto error; |
| 507 | } |
| 508 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 509 | status = setupFunctions(functions, callback, timeout, in_transactionId); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 510 | if (status != Status::SUCCESS) { |
| 511 | goto error; |
| 512 | } |
| 513 | |
Maciej Żenczykowski | a24469c | 2021-06-25 11:42:01 -0700 | [diff] [blame] | 514 | if (functions & GadgetFunction::NCM) { |
Ray Chi | ba3dc9b | 2022-03-01 21:57:03 +0800 | [diff] [blame] | 515 | if (!mGadgetIrqPath.empty()) { |
| 516 | if (!WriteStringToFile(BIG_CORE, mGadgetIrqPath)) |
| 517 | ALOGI("Cannot move gadget IRQ to big core, path:%s", mGadgetIrqPath.c_str()); |
| 518 | } |
Maciej Żenczykowski | a24469c | 2021-06-25 11:42:01 -0700 | [diff] [blame] | 519 | } else { |
Ray Chi | ba3dc9b | 2022-03-01 21:57:03 +0800 | [diff] [blame] | 520 | if (!mGadgetIrqPath.empty()) { |
| 521 | if (!WriteStringToFile(MEDIUM_CORE, mGadgetIrqPath)) |
| 522 | ALOGI("Cannot move gadget IRQ to medium core, path:%s", mGadgetIrqPath.c_str()); |
| 523 | } |
Maciej Żenczykowski | a24469c | 2021-06-25 11:42:01 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Ricky Niu | ebd7fca | 2022-05-09 10:14:20 +0800 | [diff] [blame] | 526 | if (ReadFileToString(CURRENT_USB_TYPE_PATH, ¤t_usb_type)) |
| 527 | current_usb_type = Trim(current_usb_type); |
| 528 | |
| 529 | if (ReadFileToString(CURRENT_USB_POWER_OPERATION_MODE_PATH, ¤t_usb_power_operation_mode)) |
| 530 | current_usb_power_operation_mode = Trim(current_usb_power_operation_mode); |
| 531 | |
| 532 | if (functions & GadgetFunction::ACCESSORY && |
| 533 | current_usb_type == "Unknown SDP [CDP] DCP" && |
| 534 | (current_usb_power_operation_mode == "default" || |
| 535 | current_usb_power_operation_mode == "1.5A")) { |
| 536 | if (!WriteStringToFile("1300000", accessoryCurrentLimitPath)) { |
| 537 | ALOGI("Write 1.3A to limit current fail"); |
| 538 | } else { |
| 539 | if (!WriteStringToFile("1", accessoryCurrentLimitEnablePath)) { |
| 540 | ALOGI("Enable limit current fail"); |
| 541 | } |
| 542 | } |
| 543 | } else { |
| 544 | if (!WriteStringToFile("0", accessoryCurrentLimitEnablePath)) |
| 545 | ALOGI("unvote accessory limit current failed"); |
| 546 | } |
| 547 | |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 548 | ALOGI("Usb Gadget setcurrent functions called successfully"); |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 549 | return ScopedAStatus::fromServiceSpecificErrorWithMessage( |
| 550 | -1, "Usb Gadget setcurrent functions called successfully"); |
| 551 | |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 552 | |
| 553 | error: |
| 554 | ALOGI("Usb Gadget setcurrent functions failed"); |
| 555 | if (callback == NULL) |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 556 | return ScopedAStatus::fromServiceSpecificErrorWithMessage( |
| 557 | -1, "Usb Gadget setcurrent functions failed"); |
| 558 | ScopedAStatus ret = callback->setCurrentUsbFunctionsCb(functions, status, in_transactionId); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 559 | if (!ret.isOk()) |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 560 | ALOGE("Error while calling setCurrentUsbFunctionsCb %s", ret.getDescription().c_str()); |
| 561 | return ScopedAStatus::fromServiceSpecificErrorWithMessage( |
| 562 | -1, "Error while calling setCurrentUsbFunctionsCb"); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 563 | } |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 564 | } // namespace gadget |
| 565 | } // namespace usb |
| 566 | } // namespace hardware |
| 567 | } // namespace android |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 568 | } // aidl |