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