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 #pragma once 17 18 #include <android-base/file.h> 19 #include <android/hardware/usb/1.2/IUsb.h> 20 #include <android/hardware/usb/1.2/IUsbCallback.h> 21 #include <android/hardware/usb/1.2/types.h> 22 #include <android/hardware/usb/1.3/IUsb.h> 23 #include <hidl/Status.h> 24 #include <pixelusb/UsbGadgetCommon.h> 25 #include <utils/Log.h> 26 27 #define UEVENT_MSG_LEN 2048 28 // The type-c stack waits for 4.5 - 5.5 secs before declaring a port non-pd. 29 // The -partner directory would not be created until this is done. 30 // Having a margin of ~3 secs for the directory and other related bookeeping 31 // structures created and uvent fired. 32 #define PORT_TYPE_TIMEOUT 8 33 34 namespace android { 35 namespace hardware { 36 namespace usb { 37 namespace V1_3 { 38 namespace implementation { 39 40 using ::android::sp; 41 using ::android::base::WriteStringToFile; 42 using ::android::hardware::hidl_array; 43 using ::android::hardware::hidl_memory; 44 using ::android::hardware::hidl_string; 45 using ::android::hardware::hidl_vec; 46 using ::android::hardware::Return; 47 using ::android::hardware::Void; 48 using ::android::hardware::usb::V1_0::PortDataRole; 49 using ::android::hardware::usb::V1_0::PortPowerRole; 50 using ::android::hardware::usb::V1_0::PortRole; 51 using ::android::hardware::usb::V1_0::PortRoleType; 52 using ::android::hardware::usb::V1_0::Status; 53 using ::android::hardware::usb::V1_1::PortMode_1_1; 54 using ::android::hardware::usb::V1_1::PortStatus_1_1; 55 using ::android::hardware::usb::V1_2::IUsbCallback; 56 using ::android::hardware::usb::V1_2::PortStatus; 57 using ::android::hardware::usb::V1_3::IUsb; 58 using ::android::hidl::base::V1_0::DebugInfo; 59 using ::android::hidl::base::V1_0::IBase; 60 61 enum class HALVersion{ 62 V1_0, 63 V1_1, 64 V1_2, 65 V1_3 66 }; 67 68 constexpr char kGadgetName[] = "a600000.dwc3"; 69 #define SOC_PATH "/sys/devices/platform/soc/a600000.ssusb/" 70 #define ID_PATH SOC_PATH "id" 71 #define VBUS_PATH SOC_PATH "b_sess" 72 #define USB_DATA_PATH SOC_PATH "usb_data_enabled" 73 74 struct Usb : public IUsb { 75 Usb(); 76 77 Return<void> switchRole(const hidl_string &portName, const V1_0::PortRole &role) override; 78 Return<void> setCallback(const sp<V1_0::IUsbCallback> &callback) override; 79 Return<void> queryPortStatus() override; 80 Return<void> enableContaminantPresenceDetection(const hidl_string& portName, bool enable); 81 Return<void> enableContaminantPresenceProtection(const hidl_string& portName, bool enable); 82 Return<bool> enableUsbDataSignal(bool enable) override; 83 84 sp<V1_0::IUsbCallback> mCallback_1_0; 85 // Protects mCallback variable 86 pthread_mutex_t mLock; 87 // Protects roleSwitch operation 88 pthread_mutex_t mRoleSwitchLock; 89 // Threads waiting for the partner to come back wait here 90 pthread_cond_t mPartnerCV; 91 // lock protecting mPartnerCV 92 pthread_mutex_t mPartnerLock; 93 // Variable to signal partner coming back online after type switch 94 bool mPartnerUp; 95 96 private: 97 pthread_t mPoll; 98 }; 99 100 } // namespace implementation 101 } // namespace V1_3 102 } // namespace usb 103 } // namespace hardware 104 } // namespace android 105