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 #ifndef ANDROID_HARDWARE_USB_GADGET_V1_1_USBGADGET_H
18 #define ANDROID_HARDWARE_USB_GADGET_V1_1_USBGADGET_H
19 
20 #include <UsbGadgetCommon.h>
21 #include <android-base/file.h>
22 #include <android-base/properties.h>
23 #include <android-base/unique_fd.h>
24 #include <android/hardware/usb/gadget/1.1/IUsbGadget.h>
25 #include <hidl/MQDescriptor.h>
26 #include <hidl/Status.h>
27 #include <sys/epoll.h>
28 #include <sys/eventfd.h>
29 #include <utils/Log.h>
30 #include <chrono>
31 #include <condition_variable>
32 #include <mutex>
33 #include <string>
34 #include <thread>
35 
36 namespace android {
37 namespace hardware {
38 namespace usb {
39 namespace gadget {
40 namespace V1_1 {
41 namespace implementation {
42 
43 using ::android::sp;
44 using ::android::base::GetProperty;
45 using ::android::base::SetProperty;
46 using ::android::base::unique_fd;
47 using ::android::base::WriteStringToFile;
48 using ::android::hardware::hidl_array;
49 using ::android::hardware::hidl_memory;
50 using ::android::hardware::hidl_string;
51 using ::android::hardware::hidl_vec;
52 using ::android::hardware::Return;
53 using ::android::hardware::Void;
54 using ::android::hardware::usb::gadget::addAdb;
55 using ::android::hardware::usb::gadget::addEpollFd;
56 using ::android::hardware::usb::gadget::getVendorFunctions;
57 using ::android::hardware::usb::gadget::kDebug;
58 using ::android::hardware::usb::gadget::kDisconnectWaitUs;
59 using ::android::hardware::usb::gadget::linkFunction;
60 using ::android::hardware::usb::gadget::MonitorFfs;
61 using ::android::hardware::usb::gadget::resetGadget;
62 using ::android::hardware::usb::gadget::setVidPid;
63 using ::android::hardware::usb::gadget::unlinkFunctions;
64 using ::std::string;
65 
66 constexpr char kGadgetName[] = "a600000.dwc3";
67 static MonitorFfs monitorFfs(kGadgetName);
68 
69 struct UsbGadget : public IUsbGadget {
70     UsbGadget();
71 
72     // Makes sure that only one request is processed at a time.
73     std::mutex mLockSetCurrentFunction;
74     uint64_t mCurrentUsbFunctions;
75     bool mCurrentUsbFunctionsApplied;
76 
77     Return<void> setCurrentUsbFunctions(uint64_t functions,
78                                         const sp<V1_0::IUsbGadgetCallback>& callback,
79                                         uint64_t timeout) override;
80 
81     Return<void> getCurrentUsbFunctions(const sp<V1_0::IUsbGadgetCallback>& callback) override;
82 
83     Return<Status> reset() override;
84 
85   private:
86     V1_0::Status tearDownGadget();
87     V1_0::Status setupFunctions(uint64_t functions, const sp<V1_0::IUsbGadgetCallback>& callback,
88                                 uint64_t timeout);
89 };
90 
91 }  // namespace implementation
92 }  // namespace V1_1
93 }  // namespace gadget
94 }  // namespace usb
95 }  // namespace hardware
96 }  // namespace android
97 
98 #endif  // ANDROID_HARDWARE_USB_V1_1_USBGADGET_H
99