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 <pixelusb/UsbGadgetAidlCommon.h>
30 #include <sched.h>
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 
40 namespace aidl {
41 namespace android {
42 namespace hardware {
43 namespace usb {
44 namespace gadget {
45 
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;
51 using ::android::base::GetProperty;
52 using ::android::base::SetProperty;
53 using ::android::base::ParseUint;
54 using ::android::base::unique_fd;
55 using ::android::base::ReadFileToString;
56 using ::android::base::Trim;
57 using ::android::base::WriteStringToFile;
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;
68 using ::ndk::ScopedAStatus;
69 using ::std::shared_ptr;
70 using ::std::string;
71 
72 constexpr char kGadgetName[] = "11110000.dwc3";
73 constexpr char kProcInterruptsPath[] = "/proc/interrupts";
74 constexpr char kProcIrqPath[] = "/proc/irq/";
75 constexpr char kSmpAffinityList[] = "/smp_affinity_list";
76 #ifndef UDC_PATH
77 #define UDC_PATH "/sys/class/udc/11110000.dwc3/"
78 #endif
79 static MonitorFfs monitorFfs(kGadgetName);
80 
81 #define SPEED_PATH UDC_PATH "current_speed"
82 
83 #define BIG_CORE "6"
84 #define MEDIUM_CORE "4"
85 
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 
93 struct UsbGadget : public BnUsbGadget {
94     UsbGadget();
95 
96     // Makes sure that only one request is processed at a time.
97     std::mutex mLockSetCurrentFunction;
98     std::string mGadgetIrqPath;
99     long mCurrentUsbFunctions;
100     bool mCurrentUsbFunctionsApplied;
101     UsbSpeed mUsbSpeed;
102 
103     ScopedAStatus setCurrentUsbFunctions(long functions,
104             const shared_ptr<IUsbGadgetCallback> &callback,
105             int64_t timeout, int64_t in_transactionId) override;
106 
107     ScopedAStatus getCurrentUsbFunctions(const shared_ptr<IUsbGadgetCallback> &callback,
108 	    int64_t in_transactionId) override;
109 
110     ScopedAStatus reset(const shared_ptr<IUsbGadgetCallback> &callback,
111 	    int64_t in_transactionId) override;
112 
113     ScopedAStatus getUsbSpeed(const shared_ptr<IUsbGadgetCallback> &callback,
114 	    int64_t in_transactionId) override;
115 
116     ScopedAStatus setVidPid(const char *vid,const char *pid);
117 
118     // Indicates to the kernel that the gadget service is ready and the kernel can
119     // set SDP timeout to a lower value.
120     void updateSdpEnumTimeout();
121 
122     int getI2cBusNumber();
123     std::string_view getI2cClientPath();
124 
125   private:
126     int mI2cBusNumber;
127     std::string mI2cClientPath;
128     Status tearDownGadget();
129     Status getUsbGadgetIrqPath();
130     Status setupFunctions(long functions, const shared_ptr<IUsbGadgetCallback> &callback,
131             uint64_t timeout, int64_t in_transactionId);
132 };
133 
134 }  // namespace gadget
135 }  // namespace usb
136 }  // namespace hardware
137 }  // namespace android
138 }  // aidl
139