blob: d7e20a6c284bb7c2cb619bbbc709f91bd7e5682c [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>
Ricky Niud6d0b7d2022-07-06 20:57:02 +080024#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 <pixelusb/UsbGadgetAidlCommon.h>
30#include <sched.h>
Robin Pengc2b5ca92021-02-23 20:00:28 +080031#include <sys/epoll.h>
32#include <sys/eventfd.h>
33#include <utils/Log.h>
34#include <chrono>
35#include <condition_variable>
36#include <mutex>
37#include <string>
38#include <thread>
39
Ricky Niud6d0b7d2022-07-06 20:57:02 +080040namespace aidl {
Robin Pengc2b5ca92021-02-23 20:00:28 +080041namespace android {
42namespace hardware {
43namespace usb {
44namespace gadget {
Robin Pengc2b5ca92021-02-23 20:00:28 +080045
Ricky Niud6d0b7d2022-07-06 20:57:02 +080046using ::aidl::android::hardware::usb::gadget::GadgetFunction;
47using ::aidl::android::hardware::usb::gadget::IUsbGadgetCallback;
48using ::aidl::android::hardware::usb::gadget::IUsbGadget;
49using ::aidl::android::hardware::usb::gadget::Status;
50using ::aidl::android::hardware::usb::gadget::UsbSpeed;
Robin Pengc2b5ca92021-02-23 20:00:28 +080051using ::android::base::GetProperty;
52using ::android::base::SetProperty;
Ray Chiba3dc9b2022-03-01 21:57:03 +080053using ::android::base::ParseUint;
Robin Pengc2b5ca92021-02-23 20:00:28 +080054using ::android::base::unique_fd;
55using ::android::base::ReadFileToString;
56using ::android::base::Trim;
57using ::android::base::WriteStringToFile;
Robin Pengc2b5ca92021-02-23 20:00:28 +080058using ::android::hardware::google::pixel::usb::addAdb;
59using ::android::hardware::google::pixel::usb::addEpollFd;
60using ::android::hardware::google::pixel::usb::getVendorFunctions;
61using ::android::hardware::google::pixel::usb::kDebug;
62using ::android::hardware::google::pixel::usb::kDisconnectWaitUs;
63using ::android::hardware::google::pixel::usb::linkFunction;
64using ::android::hardware::google::pixel::usb::MonitorFfs;
65using ::android::hardware::google::pixel::usb::resetGadget;
66using ::android::hardware::google::pixel::usb::setVidPid;
67using ::android::hardware::google::pixel::usb::unlinkFunctions;
Ricky Niud6d0b7d2022-07-06 20:57:02 +080068using ::ndk::ScopedAStatus;
69using ::std::shared_ptr;
Robin Pengc2b5ca92021-02-23 20:00:28 +080070using ::std::string;
71
Robin Pengc2b5ca92021-02-23 20:00:28 +080072constexpr char kGadgetName[] = "11110000.dwc3";
Ray Chiba3dc9b2022-03-01 21:57:03 +080073constexpr char kProcInterruptsPath[] = "/proc/interrupts";
74constexpr char kProcIrqPath[] = "/proc/irq/";
75constexpr char kSmpAffinityList[] = "/smp_affinity_list";
Ricky Niu88313cf2021-03-31 15:54:30 +080076#ifndef UDC_PATH
Robin Pengc2b5ca92021-02-23 20:00:28 +080077#define UDC_PATH "/sys/class/udc/11110000.dwc3/"
Robin Pengc2b5ca92021-02-23 20:00:28 +080078#endif
Ray Chi387995b2021-05-12 23:19:48 +080079static MonitorFfs monitorFfs(kGadgetName);
Robin Pengc2b5ca92021-02-23 20:00:28 +080080
81#define SPEED_PATH UDC_PATH "current_speed"
82
Ray Chiba3dc9b2022-03-01 21:57:03 +080083#define BIG_CORE "6"
84#define MEDIUM_CORE "4"
85
Ricky Niuebd7fca2022-05-09 10:14:20 +080086#define POWER_SUPPLY_PATH "/sys/class/power_supply/usb/"
87#define USB_PORT0_PATH "/sys/class/typec/port0/"
88
89#define CURRENT_MAX_PATH POWER_SUPPLY_PATH "current_max"
90#define CURRENT_USB_TYPE_PATH POWER_SUPPLY_PATH "usb_type"
91#define CURRENT_USB_POWER_OPERATION_MODE_PATH USB_PORT0_PATH "power_operation_mode"
92
Ricky Niud6d0b7d2022-07-06 20:57:02 +080093struct UsbGadget : public BnUsbGadget {
Robin Pengc2b5ca92021-02-23 20:00:28 +080094 UsbGadget();
95
96 // Makes sure that only one request is processed at a time.
97 std::mutex mLockSetCurrentFunction;
Ray Chiba3dc9b2022-03-01 21:57:03 +080098 std::string mGadgetIrqPath;
Ricky Niud6d0b7d2022-07-06 20:57:02 +080099 long mCurrentUsbFunctions;
Robin Pengc2b5ca92021-02-23 20:00:28 +0800100 bool mCurrentUsbFunctionsApplied;
101 UsbSpeed mUsbSpeed;
102
Ricky Niud6d0b7d2022-07-06 20:57:02 +0800103 ScopedAStatus setCurrentUsbFunctions(long functions,
104 const shared_ptr<IUsbGadgetCallback> &callback,
105 int64_t timeout, int64_t in_transactionId) override;
Robin Pengc2b5ca92021-02-23 20:00:28 +0800106
Ricky Niud6d0b7d2022-07-06 20:57:02 +0800107 ScopedAStatus getCurrentUsbFunctions(const shared_ptr<IUsbGadgetCallback> &callback,
108 int64_t in_transactionId) override;
Robin Pengc2b5ca92021-02-23 20:00:28 +0800109
Ricky Niud6d0b7d2022-07-06 20:57:02 +0800110 ScopedAStatus reset() override;
Robin Pengc2b5ca92021-02-23 20:00:28 +0800111
Ricky Niud6d0b7d2022-07-06 20:57:02 +0800112 ScopedAStatus getUsbSpeed(const shared_ptr<IUsbGadgetCallback> &callback,
113 int64_t in_transactionId) override;
114
115 ScopedAStatus setVidPid(const char *vid,const char *pid);
Robin Pengc2b5ca92021-02-23 20:00:28 +0800116
117 private:
118 Status tearDownGadget();
Ray Chiba3dc9b2022-03-01 21:57:03 +0800119 Status getUsbGadgetIrqPath();
Ricky Niud6d0b7d2022-07-06 20:57:02 +0800120 Status setupFunctions(long functions, const shared_ptr<IUsbGadgetCallback> &callback,
121 uint64_t timeout, int64_t in_transactionId);
Robin Pengc2b5ca92021-02-23 20:00:28 +0800122};
123
Robin Pengc2b5ca92021-02-23 20:00:28 +0800124} // namespace gadget
125} // namespace usb
126} // namespace hardware
127} // namespace android
Ricky Niud6d0b7d2022-07-06 20:57:02 +0800128} // aidl