1 /*
2  * Copyright (C) 2021 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 <aidl/android/hardware/usb/BnUsb.h>
21 #include <aidl/android/hardware/usb/BnUsbCallback.h>
22 #include <pixelusb/UsbOverheatEvent.h>
23 #include <utils/Log.h>
24 #include <UsbDataSessionMonitor.h>
25 
26 #define UEVENT_MSG_LEN 2048
27 // The type-c stack waits for 4.5 - 5.5 secs before declaring a port non-pd.
28 // The -partner directory would not be created until this is done.
29 // Having a margin of ~3 secs for the directory and other related bookeeping
30 // structures created and uvent fired.
31 #define PORT_TYPE_TIMEOUT 8
32 
33 namespace aidl {
34 namespace android {
35 namespace hardware {
36 namespace usb {
37 
38 using ::aidl::android::hardware::usb::IUsbCallback;
39 using ::aidl::android::hardware::usb::PortRole;
40 using ::android::base::ReadFileToString;
41 using ::android::base::WriteStringToFile;
42 using ::android::hardware::google::pixel::usb::UsbOverheatEvent;
43 using ::android::hardware::google::pixel::usb::ZoneInfo;
44 using ::android::hardware::thermal::V2_0::TemperatureType;
45 using ::android::hardware::thermal::V2_0::ThrottlingSeverity;
46 using ::android::sp;
47 using ::ndk::ScopedAStatus;
48 using ::std::shared_ptr;
49 using ::std::string;
50 
51 constexpr char kGadgetName[] = "11110000.dwc3";
52 #define NEW_UDC_PATH "/sys/devices/platform/11110000.usb/"
53 
54 #define ID_PATH NEW_UDC_PATH "dwc3_exynos_otg_id"
55 #define VBUS_PATH NEW_UDC_PATH "dwc3_exynos_otg_b_sess"
56 #define USB_DATA_PATH NEW_UDC_PATH "usb_data_enabled"
57 
58 #define ROLE_SWAP_RETRY_MS 700
59 
60 struct Usb : public BnUsb {
61     Usb();
62 
63     ScopedAStatus enableContaminantPresenceDetection(const std::string& in_portName,
64             bool in_enable, int64_t in_transactionId) override;
65     ScopedAStatus queryPortStatus(int64_t in_transactionId) override;
66     ScopedAStatus setCallback(const shared_ptr<IUsbCallback>& in_callback) override;
67     ScopedAStatus switchRole(const string& in_portName, const PortRole& in_role,
68             int64_t in_transactionId) override;
69     ScopedAStatus enableUsbData(const string& in_portName, bool in_enable,
70             int64_t in_transactionId) override;
71     ScopedAStatus enableUsbDataWhileDocked(const string& in_portName,
72             int64_t in_transactionId) override;
73     ScopedAStatus limitPowerTransfer(const string& in_portName, bool in_limit,
74         int64_t in_transactionId) override;
75     ScopedAStatus resetUsbPort(const string& in_portName, int64_t in_transactionId) override;
76 
77     std::shared_ptr<::aidl::android::hardware::usb::IUsbCallback> mCallback;
78     // Protects mCallback variable
79     pthread_mutex_t mLock;
80     // Protects roleSwitch operation
81     pthread_mutex_t mRoleSwitchLock;
82     // Threads waiting for the partner to come back wait here
83     pthread_cond_t mPartnerCV;
84     // lock protecting mPartnerCV
85     pthread_mutex_t mPartnerLock;
86     // Variable to signal partner coming back online after type switch
87     bool mPartnerUp;
88 
89     // Report usb data session event and data incompliance warnings
90     UsbDataSessionMonitor mUsbDataSessionMonitor;
91     // Usb Overheat object for push suez event
92     UsbOverheatEvent mOverheat;
93     // Temperature when connected
94     float mPluggedTemperatureCelsius;
95     // Usb Data status
96     bool mUsbDataEnabled;
97     int getI2cBusNumber();
98     std::string_view getI2cClientPath();
99 
100   private:
101     pthread_t mPoll;
102     int mI2cBusNumber;
103     std::string mI2cClientPath;
104 };
105 
106 } // namespace usb
107 } // namespace hardware
108 } // namespace android
109 } // aidl
110