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