Badhri Jagan Sridharan | 72a02ab | 2018-10-22 11:06:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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" |
| 18 | |
| 19 | #include "include/pixelusb/UsbGadgetCommon.h" |
| 20 | |
| 21 | namespace android { |
| 22 | namespace hardware { |
| 23 | namespace google { |
| 24 | namespace pixel { |
| 25 | namespace usb { |
| 26 | |
| 27 | int unlinkFunctions(const char *path) { |
| 28 | DIR *config = opendir(path); |
| 29 | struct dirent *function; |
| 30 | char filepath[kMaxFilePathLength]; |
| 31 | int ret = 0; |
| 32 | |
| 33 | if (config == NULL) |
| 34 | return -1; |
| 35 | |
| 36 | // d_type does not seems to be supported in /config |
| 37 | // so filtering by name. |
| 38 | while (((function = readdir(config)) != NULL)) { |
| 39 | if ((strstr(function->d_name, FUNCTION_NAME) == NULL)) |
| 40 | continue; |
| 41 | // build the path for each file in the folder. |
Josh Gao | af10863 | 2019-09-06 12:37:53 -0700 | [diff] [blame] | 42 | snprintf(filepath, kMaxFilePathLength, "%s/%s", path, function->d_name); |
Badhri Jagan Sridharan | 72a02ab | 2018-10-22 11:06:00 -0700 | [diff] [blame] | 43 | ret = remove(filepath); |
| 44 | if (ret) { |
| 45 | ALOGE("Unable remove file %s errno:%d", filepath, errno); |
| 46 | break; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | closedir(config); |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | int addEpollFd(const unique_fd &epfd, const unique_fd &fd) { |
| 55 | struct epoll_event event; |
| 56 | int ret; |
| 57 | |
| 58 | event.data.fd = fd; |
| 59 | event.events = EPOLLIN; |
| 60 | |
| 61 | ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event); |
| 62 | if (ret) |
| 63 | ALOGE("epoll_ctl error %d", errno); |
| 64 | |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | int linkFunction(const char *function, int index) { |
| 69 | char functionPath[kMaxFilePathLength]; |
| 70 | char link[kMaxFilePathLength]; |
| 71 | |
Josh Gao | af10863 | 2019-09-06 12:37:53 -0700 | [diff] [blame] | 72 | snprintf(functionPath, kMaxFilePathLength, "%s%s", FUNCTIONS_PATH, function); |
| 73 | snprintf(link, kMaxFilePathLength, "%s%d", FUNCTION_PATH, index); |
Badhri Jagan Sridharan | 72a02ab | 2018-10-22 11:06:00 -0700 | [diff] [blame] | 74 | if (symlink(functionPath, link)) { |
| 75 | ALOGE("Cannot create symlink %s -> %s errno:%d", link, functionPath, errno); |
| 76 | return -1; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | Status setVidPid(const char *vid, const char *pid) { |
| 82 | if (!WriteStringToFile(vid, VENDOR_ID_PATH)) |
| 83 | return Status::ERROR; |
| 84 | |
| 85 | if (!WriteStringToFile(pid, PRODUCT_ID_PATH)) |
| 86 | return Status::ERROR; |
| 87 | |
| 88 | return Status::SUCCESS; |
| 89 | } |
| 90 | |
| 91 | std::string getVendorFunctions() { |
| 92 | if (GetProperty(kBuildType, "") == "user") |
| 93 | return "user"; |
| 94 | |
| 95 | std::string bootMode = GetProperty(PERSISTENT_BOOT_MODE, ""); |
| 96 | std::string persistVendorFunctions = GetProperty(kPersistentVendorConfig, ""); |
| 97 | std::string vendorFunctions = GetProperty(kVendorConfig, ""); |
| 98 | std::string ret = ""; |
| 99 | |
| 100 | if (vendorFunctions != "") { |
| 101 | ret = vendorFunctions; |
raychi | 3b8465c | 2019-01-04 16:49:48 +0800 | [diff] [blame] | 102 | } else if (bootMode == "usbradio" || bootMode == "factory" || bootMode == "ffbm-00" || |
Puma Hsu | b042703 | 2021-08-29 22:16:47 +0800 | [diff] [blame] | 103 | bootMode == "ffbm-01" || bootMode == "usbuwb") { |
Badhri Jagan Sridharan | 72a02ab | 2018-10-22 11:06:00 -0700 | [diff] [blame] | 104 | if (persistVendorFunctions != "") |
| 105 | ret = persistVendorFunctions; |
| 106 | else |
| 107 | ret = "diag"; |
| 108 | // vendor.usb.config will reflect the current configured functions |
| 109 | SetProperty(kVendorConfig, ret); |
| 110 | } |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | Status resetGadget() { |
| 116 | ALOGI("setCurrentUsbFunctions None"); |
| 117 | |
| 118 | if (!WriteStringToFile("none", PULLUP_PATH)) |
| 119 | ALOGI("Gadget cannot be pulled down"); |
| 120 | |
| 121 | if (!WriteStringToFile("0", DEVICE_CLASS_PATH)) |
| 122 | return Status::ERROR; |
| 123 | |
| 124 | if (!WriteStringToFile("0", DEVICE_SUB_CLASS_PATH)) |
| 125 | return Status::ERROR; |
| 126 | |
| 127 | if (!WriteStringToFile("0", DEVICE_PROTOCOL_PATH)) |
| 128 | return Status::ERROR; |
| 129 | |
| 130 | if (!WriteStringToFile("0", DESC_USE_PATH)) |
| 131 | return Status::ERROR; |
| 132 | |
| 133 | if (unlinkFunctions(CONFIG_PATH)) |
| 134 | return Status::ERROR; |
| 135 | |
| 136 | return Status::SUCCESS; |
| 137 | } |
| 138 | |
| 139 | Status addGenericAndroidFunctions(MonitorFfs *monitorFfs, uint64_t functions, bool *ffsEnabled, |
| 140 | int *functionCount) { |
| 141 | if (((functions & GadgetFunction::MTP) != 0)) { |
| 142 | *ffsEnabled = true; |
| 143 | ALOGI("setCurrentUsbFunctions mtp"); |
| 144 | if (!WriteStringToFile("1", DESC_USE_PATH)) |
| 145 | return Status::ERROR; |
| 146 | |
| 147 | if (!monitorFfs->addInotifyFd("/dev/usb-ffs/mtp/")) |
| 148 | return Status::ERROR; |
| 149 | |
| 150 | if (linkFunction("ffs.mtp", (*functionCount)++)) |
| 151 | return Status::ERROR; |
| 152 | |
| 153 | // Add endpoints to be monitored. |
| 154 | monitorFfs->addEndPoint("/dev/usb-ffs/mtp/ep1"); |
| 155 | monitorFfs->addEndPoint("/dev/usb-ffs/mtp/ep2"); |
| 156 | monitorFfs->addEndPoint("/dev/usb-ffs/mtp/ep3"); |
| 157 | } else if (((functions & GadgetFunction::PTP) != 0)) { |
| 158 | *ffsEnabled = true; |
| 159 | ALOGI("setCurrentUsbFunctions ptp"); |
| 160 | if (!WriteStringToFile("1", DESC_USE_PATH)) |
| 161 | return Status::ERROR; |
| 162 | |
| 163 | if (!monitorFfs->addInotifyFd("/dev/usb-ffs/ptp/")) |
| 164 | return Status::ERROR; |
| 165 | |
| 166 | if (linkFunction("ffs.ptp", (*functionCount)++)) |
| 167 | return Status::ERROR; |
| 168 | |
| 169 | // Add endpoints to be monitored. |
| 170 | monitorFfs->addEndPoint("/dev/usb-ffs/ptp/ep1"); |
| 171 | monitorFfs->addEndPoint("/dev/usb-ffs/ptp/ep2"); |
| 172 | monitorFfs->addEndPoint("/dev/usb-ffs/ptp/ep3"); |
| 173 | } |
| 174 | |
| 175 | if ((functions & GadgetFunction::MIDI) != 0) { |
| 176 | ALOGI("setCurrentUsbFunctions MIDI"); |
| 177 | if (linkFunction("midi.gs5", (*functionCount)++)) |
| 178 | return Status::ERROR; |
| 179 | } |
| 180 | |
| 181 | if ((functions & GadgetFunction::ACCESSORY) != 0) { |
| 182 | ALOGI("setCurrentUsbFunctions Accessory"); |
| 183 | if (linkFunction("accessory.gs2", (*functionCount)++)) |
| 184 | return Status::ERROR; |
| 185 | } |
| 186 | |
| 187 | if ((functions & GadgetFunction::AUDIO_SOURCE) != 0) { |
| 188 | ALOGI("setCurrentUsbFunctions Audio Source"); |
| 189 | if (linkFunction("audio_source.gs3", (*functionCount)++)) |
| 190 | return Status::ERROR; |
| 191 | } |
| 192 | |
| 193 | if ((functions & GadgetFunction::RNDIS) != 0) { |
| 194 | ALOGI("setCurrentUsbFunctions rndis"); |
raychi | 85703de | 2020-04-16 18:46:54 +0800 | [diff] [blame] | 195 | std::string rndisFunction = GetProperty(kVendorRndisConfig, ""); |
| 196 | if (rndisFunction != "") { |
| 197 | if (linkFunction(rndisFunction.c_str(), (*functionCount)++)) |
| 198 | return Status::ERROR; |
| 199 | } else { |
| 200 | // link gsi.rndis for older pixel projects |
| 201 | if (linkFunction("gsi.rndis", (*functionCount)++)) |
| 202 | return Status::ERROR; |
| 203 | } |
Badhri Jagan Sridharan | 72a02ab | 2018-10-22 11:06:00 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | return Status::SUCCESS; |
| 207 | } |
| 208 | |
| 209 | Status addAdb(MonitorFfs *monitorFfs, int *functionCount) { |
| 210 | ALOGI("setCurrentUsbFunctions Adb"); |
Josh Gao | 5c7c6d5 | 2019-09-06 12:35:32 -0700 | [diff] [blame] | 211 | if (!WriteStringToFile("1", DESC_USE_PATH)) |
| 212 | return Status::ERROR; |
| 213 | |
Badhri Jagan Sridharan | 72a02ab | 2018-10-22 11:06:00 -0700 | [diff] [blame] | 214 | if (!monitorFfs->addInotifyFd("/dev/usb-ffs/adb/")) |
| 215 | return Status::ERROR; |
| 216 | |
| 217 | if (linkFunction("ffs.adb", (*functionCount)++)) |
| 218 | return Status::ERROR; |
| 219 | monitorFfs->addEndPoint("/dev/usb-ffs/adb/ep1"); |
| 220 | monitorFfs->addEndPoint("/dev/usb-ffs/adb/ep2"); |
| 221 | ALOGI("Service started"); |
| 222 | return Status::SUCCESS; |
| 223 | } |
| 224 | |
| 225 | } // namespace usb |
| 226 | } // namespace pixel |
| 227 | } // namespace google |
| 228 | } // namespace hardware |
| 229 | } // namespace android |