blob: e3eb3b4e60c7b7ada63e74aa172c1fe62d75dc2f [file] [log] [blame]
Robin Pengc2b5ca92021-02-23 20:00:28 +08001/*
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/strings.h>
23#include <android/hardware/usb/gadget/1.2/IUsbGadget.h>
24#include <android/hardware/usb/gadget/1.2/types.h>
25#include <hidl/MQDescriptor.h>
26#include <hidl/Status.h>
27#include <pixelusb/UsbGadgetCommon.h>
28#include <sys/epoll.h>
29#include <sys/eventfd.h>
30#include <utils/Log.h>
31#include <chrono>
32#include <condition_variable>
33#include <mutex>
34#include <string>
35#include <thread>
36
37namespace android {
38namespace hardware {
39namespace usb {
40namespace gadget {
41namespace V1_2 {
42namespace implementation {
43
44using ::android::sp;
45using ::android::base::GetProperty;
46using ::android::base::SetProperty;
47using ::android::base::unique_fd;
48using ::android::base::ReadFileToString;
49using ::android::base::Trim;
50using ::android::base::WriteStringToFile;
51using ::android::hardware::hidl_array;
52using ::android::hardware::hidl_memory;
53using ::android::hardware::hidl_string;
54using ::android::hardware::hidl_vec;
55using ::android::hardware::Return;
56using ::android::hardware::Void;
57using ::android::hardware::google::pixel::usb::addAdb;
58using ::android::hardware::google::pixel::usb::addEpollFd;
59using ::android::hardware::google::pixel::usb::getVendorFunctions;
60using ::android::hardware::google::pixel::usb::kDebug;
61using ::android::hardware::google::pixel::usb::kDisconnectWaitUs;
62using ::android::hardware::google::pixel::usb::linkFunction;
63using ::android::hardware::google::pixel::usb::MonitorFfs;
64using ::android::hardware::google::pixel::usb::resetGadget;
65using ::android::hardware::google::pixel::usb::setVidPid;
66using ::android::hardware::google::pixel::usb::unlinkFunctions;
67using ::android::hardware::usb::gadget::V1_0::Status;
68using ::android::hardware::usb::gadget::V1_0::IUsbGadgetCallback;
69using ::android::hardware::usb::gadget::V1_2::IUsbGadget;
70using ::android::hardware::usb::gadget::V1_2::GadgetFunction;
71using ::std::string;
72
Robin Pengc2b5ca92021-02-23 20:00:28 +080073constexpr char kGadgetName[] = "11110000.dwc3";
Ricky Niu88313cf2021-03-31 15:54:30 +080074#ifndef UDC_PATH
Robin Pengc2b5ca92021-02-23 20:00:28 +080075#define UDC_PATH "/sys/class/udc/11110000.dwc3/"
Robin Pengc2b5ca92021-02-23 20:00:28 +080076#endif
Ray Chi72ef08c2021-04-14 18:03:49 +080077constexpr char kExtconUsbState[] = "/sys/class/extcon/extcon0/cable.0/state";
78static MonitorFfs monitorFfs(kGadgetName, kExtconUsbState);
Robin Pengc2b5ca92021-02-23 20:00:28 +080079
80#define SPEED_PATH UDC_PATH "current_speed"
81
82struct UsbGadget : public IUsbGadget {
83 UsbGadget();
84
85 // Makes sure that only one request is processed at a time.
86 std::mutex mLockSetCurrentFunction;
87 uint64_t mCurrentUsbFunctions;
88 bool mCurrentUsbFunctionsApplied;
89 UsbSpeed mUsbSpeed;
90
91 Return<void> setCurrentUsbFunctions(uint64_t functions,
92 const sp<V1_0::IUsbGadgetCallback> &callback,
93 uint64_t timeout) override;
94
95 Return<void> getCurrentUsbFunctions(const sp<V1_0::IUsbGadgetCallback> &callback) override;
96
97 Return<Status> reset() override;
98
99 Return<void> getUsbSpeed(const sp<V1_2::IUsbGadgetCallback> &callback) override;
100
101 private:
102 Status tearDownGadget();
103 Status setupFunctions(uint64_t functions, const sp<V1_0::IUsbGadgetCallback> &callback,
104 uint64_t timeout);
105};
106
107} // namespace implementation
108} // namespace V1_2
109} // namespace gadget
110} // namespace usb
111} // namespace hardware
112} // namespace android