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