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 17 #pragma once 18 19 #include <android-base/file.h> 20 #include <android-base/properties.h> 21 #include <android-base/unique_fd.h> 22 #include <android-base/parseint.h> 23 #include <android-base/strings.h> 24 #include <aidl/android/hardware/usb/gadget/BnUsbGadget.h> 25 #include <aidl/android/hardware/usb/gadget/BnUsbGadgetCallback.h> 26 #include <aidl/android/hardware/usb/gadget/GadgetFunction.h> 27 #include <aidl/android/hardware/usb/gadget/IUsbGadget.h> 28 #include <aidl/android/hardware/usb/gadget/IUsbGadgetCallback.h> 29 #include <sched.h> 30 #include <sys/epoll.h> 31 #include <sys/eventfd.h> 32 #include <utils/Log.h> 33 #include <chrono> 34 #include <condition_variable> 35 #include <mutex> 36 #include <string> 37 #include <thread> 38 39 namespace aidl { 40 namespace android { 41 namespace hardware { 42 namespace usb { 43 namespace gadget { 44 45 using ::aidl::android::hardware::usb::gadget::GadgetFunction; 46 using ::aidl::android::hardware::usb::gadget::IUsbGadgetCallback; 47 using ::aidl::android::hardware::usb::gadget::IUsbGadget; 48 using ::aidl::android::hardware::usb::gadget::Status; 49 using ::aidl::android::hardware::usb::gadget::UsbSpeed; 50 using ::android::base::GetProperty; 51 using ::android::base::SetProperty; 52 using ::android::base::ParseUint; 53 using ::android::base::unique_fd; 54 using ::android::base::ReadFileToString; 55 using ::android::base::Trim; 56 using ::android::base::WriteStringToFile; 57 using ::ndk::ScopedAStatus; 58 using ::std::shared_ptr; 59 using ::std::string; 60 61 constexpr char kGadgetName[] = "11110000.dwc3"; 62 constexpr char kProcInterruptsPath[] = "/proc/interrupts"; 63 constexpr char kProcIrqPath[] = "/proc/irq/"; 64 constexpr char kSmpAffinityList[] = "/smp_affinity_list"; 65 #ifndef UDC_PATH 66 #define UDC_PATH "/sys/class/udc/11110000.dwc3/" 67 #endif 68 //static MonitorFfs monitorFfs(kGadgetName); 69 70 #define SPEED_PATH UDC_PATH "current_speed" 71 72 #define BIG_CORE "6" 73 #define MEDIUM_CORE "4" 74 75 #define POWER_SUPPLY_PATH "/sys/class/power_supply/usb/" 76 #define USB_PORT0_PATH "/sys/class/typec/port0/" 77 78 #define CURRENT_MAX_PATH POWER_SUPPLY_PATH "current_max" 79 #define CURRENT_USB_TYPE_PATH POWER_SUPPLY_PATH "usb_type" 80 #define CURRENT_USB_POWER_OPERATION_MODE_PATH USB_PORT0_PATH "power_operation_mode" 81 82 struct UsbGadget : public BnUsbGadget { 83 UsbGadget(); 84 85 // Makes sure that only one request is processed at a time. 86 std::mutex mLockSetCurrentFunction; 87 std::string mGadgetIrqPath; 88 long mCurrentUsbFunctions; 89 bool mCurrentUsbFunctionsApplied; 90 UsbSpeed mUsbSpeed; 91 92 ScopedAStatus setCurrentUsbFunctions(int64_t functions, 93 const shared_ptr<IUsbGadgetCallback> &callback, 94 int64_t timeoutMs, int64_t in_transactionId) override; 95 96 ScopedAStatus getCurrentUsbFunctions(const shared_ptr<IUsbGadgetCallback> &callback, 97 int64_t in_transactionId) override; 98 99 ScopedAStatus reset(const shared_ptr<IUsbGadgetCallback> &callback, 100 int64_t in_transactionId) override; 101 102 ScopedAStatus getUsbSpeed(const shared_ptr<IUsbGadgetCallback> &callback, 103 int64_t in_transactionId) override; 104 105 private: 106 Status tearDownGadget(); 107 Status getUsbGadgetIrqPath(); 108 Status setupFunctions(long functions, const shared_ptr<IUsbGadgetCallback> &callback, 109 uint64_t timeout, int64_t in_transactionId); 110 }; 111 112 } // namespace gadget 113 } // namespace usb 114 } // namespace hardware 115 } // namespace android 116 } // aidl 117