• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 <utils/Log.h>
23 
24 #define UEVENT_MSG_LEN 2048
25 // The type-c stack waits for 4.5 - 5.5 secs before declaring a port non-pd.
26 // The -partner directory would not be created until this is done.
27 // Having a margin of ~3 secs for the directory and other related bookeeping
28 // structures created and uvent fired.
29 #define PORT_TYPE_TIMEOUT 8
30 
31 namespace aidl {
32 namespace android {
33 namespace hardware {
34 namespace usb {
35 
36 using ::aidl::android::hardware::usb::IUsbCallback;
37 using ::aidl::android::hardware::usb::PortRole;
38 using ::android::base::ReadFileToString;
39 using ::android::base::WriteStringToFile;
40 using ::android::sp;
41 using ::ndk::ScopedAStatus;
42 using ::std::shared_ptr;
43 using ::std::string;
44 
45 constexpr char kGadgetName[] = "a600000.dwc3";
46 #define PULLUP_PATH "/config/usb_gadget/g1/UDC"
47 #define SOC_PATH "/sys/devices/platform/soc/a600000.ssusb/"
48 #define USB_DATA_PATH SOC_PATH "usb_data_enabled"
49 
50 #define USB_POWER_LIMIT_PATH "/sys/devices/platform/soc/c440000.qcom,spmi/spmi-0/spmi0-00/c440000.qcom,spmi:qcom,pm6150@0:qcom,usb-pdphy@1700/usbpd0/"
51 #define SINK_CURRENT_LIMIT_PATH USB_POWER_LIMIT_PATH "usb_limit_sink_current"
52 #define SINK_LIMIT_ENABLE_PATH USB_POWER_LIMIT_PATH "usb_limit_sink_enable"
53 #define SOURCE_LIMIT_ENABLE_PATH USB_POWER_LIMIT_PATH "usb_limit_source_enable"
54 
55 struct Usb : public BnUsb {
56     Usb();
57 
58     ScopedAStatus enableContaminantPresenceDetection(const std::string& in_portName,
59             bool in_enable, int64_t in_transactionId) override;
60     ScopedAStatus queryPortStatus(int64_t in_transactionId) override;
61     ScopedAStatus setCallback(const shared_ptr<IUsbCallback>& in_callback) override;
62     ScopedAStatus switchRole(const string& in_portName, const PortRole& in_role,
63             int64_t in_transactionId) override;
64     ScopedAStatus enableUsbData(const string& in_portName, bool in_enable,
65             int64_t in_transactionId) override;
66     ScopedAStatus enableUsbDataWhileDocked(const string& in_portName,
67             int64_t in_transactionId) override;
68     ScopedAStatus limitPowerTransfer(const string& in_portName, bool in_limit,
69             int64_t in_transactionId) override;
70     ScopedAStatus resetUsbPort(const string& in_portName, int64_t in_transactionId) override;
71 
72     std::shared_ptr<::aidl::android::hardware::usb::IUsbCallback> mCallback;
73     // Protects mCallback variable
74     pthread_mutex_t mLock;
75     // Protects roleSwitch operation
76     pthread_mutex_t mRoleSwitchLock;
77     // Threads waiting for the partner to come back wait here
78     pthread_cond_t mPartnerCV;
79     // lock protecting mPartnerCV
80     pthread_mutex_t mPartnerLock;
81     // Variable to signal partner coming back online after type switch
82     bool mPartnerUp;
83     // Usb Data status
84     bool mUsbDataEnabled;
85 
86   private:
87     pthread_t mPoll;
88 };
89 
90 } // namespace usb
91 } // namespace hardware
92 } // namespace android
93 } // aidl
94