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 #ifndef HARDWARE_GOOGLE_PIXEL_USB_USBGADGETCOMMON_H 18 #define HARDWARE_GOOGLE_PIXEL_USB_USBGADGETCOMMON_H 19 20 #include <android-base/file.h> 21 #include <android-base/properties.h> 22 #include <android-base/strings.h> 23 #include <android-base/unique_fd.h> 24 #include <android/hardware/usb/gadget/1.0/IUsbGadget.h> 25 #include <dirent.h> 26 #include <fcntl.h> 27 #include <stdio.h> 28 #include <sys/epoll.h> 29 #include <sys/eventfd.h> 30 #include <sys/inotify.h> 31 #include <sys/mount.h> 32 #include <sys/stat.h> 33 #include <sys/types.h> 34 #include <unistd.h> 35 #include <utils/Log.h> 36 37 #include <chrono> 38 #include <condition_variable> 39 #include <mutex> 40 #include <string> 41 #include <thread> 42 43 namespace android { 44 namespace hardware { 45 namespace google { 46 namespace pixel { 47 namespace usb { 48 49 constexpr int kBufferSize = 512; 50 constexpr int kMaxFilePathLength = 256; 51 constexpr int kEpollEvents = 10; 52 constexpr bool kDebug = false; 53 constexpr int kDisconnectWaitUs = 100000; 54 constexpr int kPullUpDelay = 500000; 55 constexpr int kShutdownMonitor = 100; 56 57 constexpr char kBuildType[] = "ro.build.type"; 58 constexpr char kPersistentVendorConfig[] = "persist.vendor.usb.usbradio.config"; 59 constexpr char kVendorConfig[] = "vendor.usb.config"; 60 constexpr char kVendorRndisConfig[] = "vendor.usb.rndis.config"; 61 62 #define GADGET_PATH "/config/usb_gadget/g1/" 63 #define PULLUP_PATH GADGET_PATH "UDC" 64 #define PERSISTENT_BOOT_MODE "ro.bootmode" 65 #define VENDOR_ID_PATH GADGET_PATH "idVendor" 66 #define PRODUCT_ID_PATH GADGET_PATH "idProduct" 67 #define DEVICE_CLASS_PATH GADGET_PATH "bDeviceClass" 68 #define DEVICE_SUB_CLASS_PATH GADGET_PATH "bDeviceSubClass" 69 #define DEVICE_PROTOCOL_PATH GADGET_PATH "bDeviceProtocol" 70 #define DESC_USE_PATH GADGET_PATH "os_desc/use" 71 #define OS_DESC_PATH GADGET_PATH "os_desc/b.1" 72 #define CONFIG_PATH GADGET_PATH "configs/b.1/" 73 #define FUNCTIONS_PATH GADGET_PATH "functions/" 74 #define FUNCTION_NAME "function" 75 #define FUNCTION_PATH CONFIG_PATH FUNCTION_NAME 76 #define RNDIS_PATH FUNCTIONS_PATH "gsi.rndis" 77 #define TYPEC_GADGET_MODE_DISABLE "0" 78 #define TYPEC_GADGET_MODE_ENABLE "1" 79 #define GADGET_DEACTIVATE "0" 80 #define GADGET_ACTIVATE "1" 81 82 using ::android::base::GetProperty; 83 using ::android::base::ReadFileToString; 84 using ::android::base::SetProperty; 85 using ::android::base::Trim; 86 using ::android::base::unique_fd; 87 using ::android::base::WriteStringToFile; 88 using ::android::hardware::usb::gadget::V1_0::GadgetFunction; 89 using ::android::hardware::usb::gadget::V1_0::Status; 90 91 using ::std::lock_guard; 92 using ::std::move; 93 using ::std::mutex; 94 using ::std::string; 95 using ::std::thread; 96 using ::std::unique_ptr; 97 using ::std::vector; 98 using ::std::chrono::microseconds; 99 using ::std::chrono::steady_clock; 100 using ::std::literals::chrono_literals::operator""ms; 101 102 // MonitorFfs automously manages gadget pullup by monitoring 103 // the ep file status. Restarts the usb gadget when the ep 104 // owner restarts. 105 class MonitorFfs { 106 private: 107 // Monitors the endpoints Inotify events. 108 unique_fd mInotifyFd; 109 // Control pipe for shutting down the mMonitor thread. 110 // mMonitor exits when SHUTDOWN_MONITOR is written into 111 // mEventFd/ 112 unique_fd mEventFd; 113 // Pools on mInotifyFd and mEventFd. 114 unique_fd mEpollFd; 115 vector<int> mWatchFd; 116 117 // Maintains the list of Endpoints. 118 vector<string> mEndpointList; 119 // protects the CV. 120 std::mutex mLock; 121 std::condition_variable mCv; 122 // protects mInotifyFd, mEpollFd. 123 std::mutex mLockFd; 124 125 // Flag to maintain the current status of gadget pullup. 126 bool mCurrentUsbFunctionsApplied; 127 128 // Thread object that executes the ep monitoring logic. 129 unique_ptr<thread> mMonitor; 130 // Callback to be invoked when gadget is pulled up. 131 void (*mCallback)(bool functionsApplied, void *payload); 132 void *mPayload; 133 // Name of the USB gadget. Used for pullup. 134 const char *const mGadgetName; 135 // Extcon USB state from Type-C notification. 136 const char *const mExtconTypecState; 137 // USB Gadget state from Dwc3 device. 138 const char *const mUsbGadgetState; 139 // Monitor State 140 bool mMonitorRunning; 141 142 public: 143 MonitorFfs(const char *const gadget, const char *const extconTypecState = "", 144 const char *const usbGadgetState = ""); 145 // Inits all the UniqueFds. 146 void reset(); 147 // Starts monitoring endpoints and pullup the gadget when 148 // the descriptors are written. 149 bool startMonitor(); 150 // Waits for timeout_ms for gadget pull up to happen. 151 // Returns immediately if the gadget is already pulled up. 152 bool waitForPullUp(int timeout_ms); 153 // Adds the given fd to the watch list. 154 bool addInotifyFd(string fd); 155 // Adds the given endpoint to the watch list. 156 void addEndPoint(string ep); 157 // Registers the async callback from the caller to notify the caller 158 // when the gadget pull up happens. 159 void registerFunctionsAppliedCallback(void (*callback)(bool functionsApplied, void *(payload)), 160 void *payload); 161 bool isMonitorRunning(); 162 // Ep monitoring and the gadget pull up logic. 163 static void *startMonitorFd(void *param); 164 }; 165 166 //**************** Helper functions ************************// 167 168 // Adds the given fd to the epollfd(epfd). 169 int addEpollFd(const unique_fd &epfd, const unique_fd &fd); 170 // Removes all the usb functions link in the specified path. 171 int unlinkFunctions(const char *path); 172 // Craetes a configfs link for the function. 173 int linkFunction(const char *function, int index); 174 // Sets the USB VID and PID. 175 Status setVidPid(const char *vid, const char *pid); 176 // Extracts vendor functions from the vendor init properties. 177 std::string getVendorFunctions(); 178 // Adds Adb to the usb configuration. 179 Status addAdb(MonitorFfs *monitorFfs, int *functionCount); 180 // Adds all applicable generic android usb functions other than ADB. 181 Status addGenericAndroidFunctions(MonitorFfs *monitorFfs, uint64_t functions, 182 bool *ffsEnabled, int *functionCount); 183 // Pulls down USB gadget. 184 Status resetGadget(); 185 // Update the state from typec and usb gadget 186 void updateState(const char *typecState, const char *usbGadgetState, std::string &typec_state, 187 std::string &usb_gadget_state); 188 189 } // namespace usb 190 } // namespace pixel 191 } // namespace google 192 } // namespace hardware 193 } // namespace android 194 #endif 195