blob: adcfcfa7cad164ae4303aca235200877b4bf0c72 [file] [log] [blame]
Ricky Niu73b76522022-02-10 14:52:12 +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/parseint.h>
23#include <android-base/strings.h>
24#include <aidl/android/hardware/usb/gadget/BnUsbGadget.h>
25#include <aidl/android/hardware/usb/gadget/BnUsbGadgetCallback.h>
26#include <aidl/android/hardware/usb/gadget/GadgetFunction.h>
27#include <aidl/android/hardware/usb/gadget/IUsbGadget.h>
28#include <aidl/android/hardware/usb/gadget/IUsbGadgetCallback.h>
29#include <sched.h>
30#include <sys/epoll.h>
31#include <sys/eventfd.h>
32#include <utils/Log.h>
33#include <chrono>
34#include <condition_variable>
35#include <mutex>
36#include <string>
37#include <thread>
38
39namespace aidl {
40namespace android {
41namespace hardware {
42namespace usb {
43namespace gadget {
44
45using ::aidl::android::hardware::usb::gadget::GadgetFunction;
46using ::aidl::android::hardware::usb::gadget::IUsbGadgetCallback;
47using ::aidl::android::hardware::usb::gadget::IUsbGadget;
48using ::aidl::android::hardware::usb::gadget::Status;
49using ::aidl::android::hardware::usb::gadget::UsbSpeed;
50using ::android::base::GetProperty;
51using ::android::base::SetProperty;
52using ::android::base::ParseUint;
53using ::android::base::unique_fd;
54using ::android::base::ReadFileToString;
55using ::android::base::Trim;
56using ::android::base::WriteStringToFile;
57using ::ndk::ScopedAStatus;
58using ::std::shared_ptr;
59using ::std::string;
60
61constexpr char kGadgetName[] = "11110000.dwc3";
62constexpr char kProcInterruptsPath[] = "/proc/interrupts";
63constexpr char kProcIrqPath[] = "/proc/irq/";
64constexpr char kSmpAffinityList[] = "/smp_affinity_list";
65#ifndef UDC_PATH
66#define UDC_PATH "/sys/class/udc/11110000.dwc3/"
67#endif
68//static MonitorFfs monitorFfs(kGadgetName);
69
70#define SPEED_PATH UDC_PATH "current_speed"
71
72#define BIG_CORE "6"
73#define MEDIUM_CORE "4"
74
75#define POWER_SUPPLY_PATH "/sys/class/power_supply/usb/"
76#define USB_PORT0_PATH "/sys/class/typec/port0/"
77
78#define CURRENT_MAX_PATH POWER_SUPPLY_PATH "current_max"
79#define CURRENT_USB_TYPE_PATH POWER_SUPPLY_PATH "usb_type"
80#define CURRENT_USB_POWER_OPERATION_MODE_PATH USB_PORT0_PATH "power_operation_mode"
81
82struct UsbGadget : public BnUsbGadget {
83 UsbGadget();
84
85 // Makes sure that only one request is processed at a time.
86 std::mutex mLockSetCurrentFunction;
87 std::string mGadgetIrqPath;
88 long mCurrentUsbFunctions;
89 bool mCurrentUsbFunctionsApplied;
90 UsbSpeed mUsbSpeed;
91
Ricky Niu2bbf8ee2022-12-01 15:24:44 +080092 ScopedAStatus setCurrentUsbFunctions(int64_t functions,
Ricky Niu73b76522022-02-10 14:52:12 +080093 const shared_ptr<IUsbGadgetCallback> &callback,
Ricky Niu2bbf8ee2022-12-01 15:24:44 +080094 int64_t timeoutMs, int64_t in_transactionId) override;
Ricky Niu73b76522022-02-10 14:52:12 +080095
96 ScopedAStatus getCurrentUsbFunctions(const shared_ptr<IUsbGadgetCallback> &callback,
97 int64_t in_transactionId) override;
98
99 ScopedAStatus reset() override;
100
101 ScopedAStatus getUsbSpeed(const shared_ptr<IUsbGadgetCallback> &callback,
102 int64_t in_transactionId) override;
103
104 private:
105 Status tearDownGadget();
106 Status getUsbGadgetIrqPath();
107 Status setupFunctions(long functions, const shared_ptr<IUsbGadgetCallback> &callback,
108 uint64_t timeout, int64_t in_transactionId);
109};
110
111} // namespace gadget
112} // namespace usb
113} // namespace hardware
114} // namespace android
115} // aidl