blob: f89476d8c0139c9f98ca8003129a9c6398227c78 [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>
Ray Chiba3dc9b2022-03-01 21:57:03 +080022#include <android-base/parseint.h>
Robin Pengc2b5ca92021-02-23 20:00:28 +080023#include <android-base/strings.h>
24#include <android/hardware/usb/gadget/1.2/IUsbGadget.h>
25#include <android/hardware/usb/gadget/1.2/types.h>
26#include <hidl/MQDescriptor.h>
27#include <hidl/Status.h>
28#include <pixelusb/UsbGadgetCommon.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
38namespace android {
39namespace hardware {
40namespace usb {
41namespace gadget {
42namespace V1_2 {
43namespace implementation {
44
45using ::android::sp;
46using ::android::base::GetProperty;
47using ::android::base::SetProperty;
Ray Chiba3dc9b2022-03-01 21:57:03 +080048using ::android::base::ParseUint;
Robin Pengc2b5ca92021-02-23 20:00:28 +080049using ::android::base::unique_fd;
50using ::android::base::ReadFileToString;
51using ::android::base::Trim;
52using ::android::base::WriteStringToFile;
53using ::android::hardware::hidl_array;
54using ::android::hardware::hidl_memory;
55using ::android::hardware::hidl_string;
56using ::android::hardware::hidl_vec;
57using ::android::hardware::Return;
58using ::android::hardware::Void;
59using ::android::hardware::google::pixel::usb::addAdb;
60using ::android::hardware::google::pixel::usb::addEpollFd;
61using ::android::hardware::google::pixel::usb::getVendorFunctions;
62using ::android::hardware::google::pixel::usb::kDebug;
63using ::android::hardware::google::pixel::usb::kDisconnectWaitUs;
64using ::android::hardware::google::pixel::usb::linkFunction;
65using ::android::hardware::google::pixel::usb::MonitorFfs;
66using ::android::hardware::google::pixel::usb::resetGadget;
67using ::android::hardware::google::pixel::usb::setVidPid;
68using ::android::hardware::google::pixel::usb::unlinkFunctions;
69using ::android::hardware::usb::gadget::V1_0::Status;
70using ::android::hardware::usb::gadget::V1_0::IUsbGadgetCallback;
71using ::android::hardware::usb::gadget::V1_2::IUsbGadget;
72using ::android::hardware::usb::gadget::V1_2::GadgetFunction;
73using ::std::string;
74
Robin Pengc2b5ca92021-02-23 20:00:28 +080075constexpr char kGadgetName[] = "11110000.dwc3";
Ray Chiba3dc9b2022-03-01 21:57:03 +080076constexpr char kProcInterruptsPath[] = "/proc/interrupts";
77constexpr char kProcIrqPath[] = "/proc/irq/";
78constexpr char kSmpAffinityList[] = "/smp_affinity_list";
Ricky Niu88313cf2021-03-31 15:54:30 +080079#ifndef UDC_PATH
Robin Pengc2b5ca92021-02-23 20:00:28 +080080#define UDC_PATH "/sys/class/udc/11110000.dwc3/"
Robin Pengc2b5ca92021-02-23 20:00:28 +080081#endif
Ray Chi387995b2021-05-12 23:19:48 +080082static MonitorFfs monitorFfs(kGadgetName);
Robin Pengc2b5ca92021-02-23 20:00:28 +080083
84#define SPEED_PATH UDC_PATH "current_speed"
85
Ray Chiba3dc9b2022-03-01 21:57:03 +080086#define BIG_CORE "6"
87#define MEDIUM_CORE "4"
88
Robin Pengc2b5ca92021-02-23 20:00:28 +080089struct UsbGadget : public IUsbGadget {
90 UsbGadget();
91
92 // Makes sure that only one request is processed at a time.
93 std::mutex mLockSetCurrentFunction;
Ray Chiba3dc9b2022-03-01 21:57:03 +080094 std::string mGadgetIrqPath;
Robin Pengc2b5ca92021-02-23 20:00:28 +080095 uint64_t mCurrentUsbFunctions;
96 bool mCurrentUsbFunctionsApplied;
97 UsbSpeed mUsbSpeed;
98
99 Return<void> setCurrentUsbFunctions(uint64_t functions,
100 const sp<V1_0::IUsbGadgetCallback> &callback,
101 uint64_t timeout) override;
102
103 Return<void> getCurrentUsbFunctions(const sp<V1_0::IUsbGadgetCallback> &callback) override;
104
105 Return<Status> reset() override;
106
107 Return<void> getUsbSpeed(const sp<V1_2::IUsbGadgetCallback> &callback) override;
108
109 private:
110 Status tearDownGadget();
Ray Chiba3dc9b2022-03-01 21:57:03 +0800111 Status getUsbGadgetIrqPath();
Robin Pengc2b5ca92021-02-23 20:00:28 +0800112 Status setupFunctions(uint64_t functions, const sp<V1_0::IUsbGadgetCallback> &callback,
113 uint64_t timeout);
114};
115
116} // namespace implementation
117} // namespace V1_2
118} // namespace gadget
119} // namespace usb
120} // namespace hardware
121} // namespace android