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/IUsbCallback.h> 20 #include <android/hardware/usb/1.2/types.h> 21 #include <android/hardware/usb/1.3/IUsb.h> 22 #include <pixelusb/UsbGadgetCommon.h> 23 #include <hidl/Status.h> 24 #include <utils/Log.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 android { 34 namespace hardware { 35 namespace usb { 36 namespace V1_3 { 37 namespace implementation { 38 39 using ::android::sp; 40 using ::android::base::WriteStringToFile; 41 using ::android::hardware::hidl_array; 42 using ::android::hardware::hidl_memory; 43 using ::android::hardware::hidl_string; 44 using ::android::hardware::hidl_vec; 45 using ::android::hardware::Return; 46 using ::android::hardware::Void; 47 using ::android::hardware::usb::V1_0::PortDataRole; 48 using ::android::hardware::usb::V1_0::PortPowerRole; 49 using ::android::hardware::usb::V1_0::PortRole; 50 using ::android::hardware::usb::V1_0::PortRoleType; 51 using ::android::hardware::usb::V1_0::Status; 52 using ::android::hardware::usb::V1_1::PortMode_1_1; 53 using ::android::hardware::usb::V1_1::PortStatus_1_1; 54 using ::android::hardware::usb::V1_2::PortStatus; 55 using ::android::hardware::usb::V1_2::IUsbCallback; 56 using ::android::hardware::usb::V1_3::IUsb; 57 using ::android::hidl::base::V1_0::DebugInfo; 58 using ::android::hidl::base::V1_0::IBase; 59 60 enum class HALVersion{ 61 V1_0, 62 V1_1, 63 V1_2, 64 V1_3 65 }; 66 67 constexpr char kGadgetName[] = "a600000.dwc3"; 68 #define SOC_PATH "/sys/devices/platform/soc/a600000.ssusb/" 69 #define ID_PATH SOC_PATH "id" 70 #define VBUS_PATH SOC_PATH "b_sess" 71 #define USB_DATA_PATH SOC_PATH "usb_data_enabled" 72 73 struct Usb : public IUsb { 74 Usb(); 75 76 Return<void> switchRole(const hidl_string &portName, const V1_0::PortRole &role) override; 77 Return<void> setCallback(const sp<V1_0::IUsbCallback> &callback) override; 78 Return<void> queryPortStatus() override; 79 Return<void> enableContaminantPresenceDetection(const hidl_string& portName, bool enable); 80 Return<void> enableContaminantPresenceProtection(const hidl_string& portName, bool enable); 81 Return<bool> enableUsbDataSignal(bool enable) override; 82 83 sp<V1_0::IUsbCallback> mCallback_1_0; 84 // Protects mCallback variable 85 pthread_mutex_t mLock; 86 // Protects roleSwitch operation 87 pthread_mutex_t mRoleSwitchLock; 88 // Threads waiting for the partner to come back wait here 89 pthread_cond_t mPartnerCV; 90 // lock protecting mPartnerCV 91 pthread_mutex_t mPartnerLock; 92 // Variable to signal partner coming back online after type switch 93 bool mPartnerUp; 94 95 private: 96 pthread_t mPoll; 97 }; 98 99 } // namespace implementation 100 } // namespace V1_3 101 } // namespace usb 102 } // namespace hardware 103 } // namespace android 104