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 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <android-base/file.h> |
| 20 | #include <android-base/properties.h> |
| 21 | #include <android-base/unique_fd.h> |
Ray Chi | ba3dc9b | 2022-03-01 21:57:03 +0800 | [diff] [blame] | 22 | #include <android-base/parseint.h> |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 23 | #include <android-base/strings.h> |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 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 <pixelusb/UsbGadgetAidlCommon.h> |
| 30 | #include <sched.h> |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 31 | #include <sys/epoll.h> |
| 32 | #include <sys/eventfd.h> |
| 33 | #include <utils/Log.h> |
| 34 | #include <chrono> |
| 35 | #include <condition_variable> |
| 36 | #include <mutex> |
| 37 | #include <string> |
| 38 | #include <thread> |
| 39 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 40 | namespace aidl { |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 41 | namespace android { |
| 42 | namespace hardware { |
| 43 | namespace usb { |
| 44 | namespace gadget { |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 45 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 46 | using ::aidl::android::hardware::usb::gadget::GadgetFunction; |
| 47 | using ::aidl::android::hardware::usb::gadget::IUsbGadgetCallback; |
| 48 | using ::aidl::android::hardware::usb::gadget::IUsbGadget; |
| 49 | using ::aidl::android::hardware::usb::gadget::Status; |
| 50 | using ::aidl::android::hardware::usb::gadget::UsbSpeed; |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 51 | using ::android::base::GetProperty; |
| 52 | using ::android::base::SetProperty; |
Ray Chi | ba3dc9b | 2022-03-01 21:57:03 +0800 | [diff] [blame] | 53 | using ::android::base::ParseUint; |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 54 | using ::android::base::unique_fd; |
| 55 | using ::android::base::ReadFileToString; |
| 56 | using ::android::base::Trim; |
| 57 | using ::android::base::WriteStringToFile; |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 58 | using ::android::hardware::google::pixel::usb::addAdb; |
| 59 | using ::android::hardware::google::pixel::usb::addEpollFd; |
| 60 | using ::android::hardware::google::pixel::usb::getVendorFunctions; |
| 61 | using ::android::hardware::google::pixel::usb::kDebug; |
| 62 | using ::android::hardware::google::pixel::usb::kDisconnectWaitUs; |
| 63 | using ::android::hardware::google::pixel::usb::linkFunction; |
| 64 | using ::android::hardware::google::pixel::usb::MonitorFfs; |
| 65 | using ::android::hardware::google::pixel::usb::resetGadget; |
| 66 | using ::android::hardware::google::pixel::usb::setVidPid; |
| 67 | using ::android::hardware::google::pixel::usb::unlinkFunctions; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 68 | using ::ndk::ScopedAStatus; |
| 69 | using ::std::shared_ptr; |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 70 | using ::std::string; |
| 71 | |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 72 | constexpr char kGadgetName[] = "11110000.dwc3"; |
Ray Chi | ba3dc9b | 2022-03-01 21:57:03 +0800 | [diff] [blame] | 73 | constexpr char kProcInterruptsPath[] = "/proc/interrupts"; |
| 74 | constexpr char kProcIrqPath[] = "/proc/irq/"; |
| 75 | constexpr char kSmpAffinityList[] = "/smp_affinity_list"; |
Ricky Niu | 88313cf | 2021-03-31 15:54:30 +0800 | [diff] [blame] | 76 | #ifndef UDC_PATH |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 77 | #define UDC_PATH "/sys/class/udc/11110000.dwc3/" |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 78 | #endif |
Ray Chi | 387995b | 2021-05-12 23:19:48 +0800 | [diff] [blame] | 79 | static MonitorFfs monitorFfs(kGadgetName); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 80 | |
| 81 | #define SPEED_PATH UDC_PATH "current_speed" |
| 82 | |
Ray Chi | ba3dc9b | 2022-03-01 21:57:03 +0800 | [diff] [blame] | 83 | #define BIG_CORE "6" |
| 84 | #define MEDIUM_CORE "4" |
| 85 | |
Ricky Niu | ebd7fca | 2022-05-09 10:14:20 +0800 | [diff] [blame] | 86 | #define POWER_SUPPLY_PATH "/sys/class/power_supply/usb/" |
| 87 | #define USB_PORT0_PATH "/sys/class/typec/port0/" |
| 88 | |
| 89 | #define CURRENT_MAX_PATH POWER_SUPPLY_PATH "current_max" |
| 90 | #define CURRENT_USB_TYPE_PATH POWER_SUPPLY_PATH "usb_type" |
| 91 | #define CURRENT_USB_POWER_OPERATION_MODE_PATH USB_PORT0_PATH "power_operation_mode" |
| 92 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 93 | struct UsbGadget : public BnUsbGadget { |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 94 | UsbGadget(); |
| 95 | |
| 96 | // Makes sure that only one request is processed at a time. |
| 97 | std::mutex mLockSetCurrentFunction; |
Ray Chi | ba3dc9b | 2022-03-01 21:57:03 +0800 | [diff] [blame] | 98 | std::string mGadgetIrqPath; |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 99 | long mCurrentUsbFunctions; |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 100 | bool mCurrentUsbFunctionsApplied; |
| 101 | UsbSpeed mUsbSpeed; |
| 102 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 103 | ScopedAStatus setCurrentUsbFunctions(long functions, |
| 104 | const shared_ptr<IUsbGadgetCallback> &callback, |
| 105 | int64_t timeout, int64_t in_transactionId) override; |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 106 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 107 | ScopedAStatus getCurrentUsbFunctions(const shared_ptr<IUsbGadgetCallback> &callback, |
| 108 | int64_t in_transactionId) override; |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 109 | |
Ricky Niu | 744f214 | 2023-01-31 18:01:06 +0800 | [diff] [blame^] | 110 | ScopedAStatus reset(const shared_ptr<IUsbGadgetCallback> &callback, |
| 111 | int64_t in_transactionId) override; |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 112 | |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 113 | ScopedAStatus getUsbSpeed(const shared_ptr<IUsbGadgetCallback> &callback, |
| 114 | int64_t in_transactionId) override; |
| 115 | |
| 116 | ScopedAStatus setVidPid(const char *vid,const char *pid); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 117 | |
| 118 | private: |
| 119 | Status tearDownGadget(); |
Ray Chi | ba3dc9b | 2022-03-01 21:57:03 +0800 | [diff] [blame] | 120 | Status getUsbGadgetIrqPath(); |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 121 | Status setupFunctions(long functions, const shared_ptr<IUsbGadgetCallback> &callback, |
| 122 | uint64_t timeout, int64_t in_transactionId); |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 123 | }; |
| 124 | |
Robin Peng | c2b5ca9 | 2021-02-23 20:00:28 +0800 | [diff] [blame] | 125 | } // namespace gadget |
| 126 | } // namespace usb |
| 127 | } // namespace hardware |
| 128 | } // namespace android |
Ricky Niu | d6d0b7d | 2022-07-06 20:57:02 +0800 | [diff] [blame] | 129 | } // aidl |