• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "android.hardware.tv.cec@1.0-impl"
17 
18 #include <android-base/logging.h>
19 #include <errno.h>
20 #include <linux/cec.h>
21 #include <linux/ioctl.h>
22 #include <sys/eventfd.h>
23 #include <algorithm>
24 
25 #include "HdmiCecPort.h"
26 
27 namespace android {
28 namespace hardware {
29 namespace tv {
30 namespace cec {
31 namespace V1_0 {
32 namespace implementation {
33 
HdmiCecPort(unsigned int portId)34 HdmiCecPort::HdmiCecPort(unsigned int portId) {
35     mPortId = portId;
36     mCecFd = -1;
37     mExitFd = -1;
38 }
39 
~HdmiCecPort()40 HdmiCecPort::~HdmiCecPort() {
41     release();
42 }
43 
44 // Initialise the cec file descriptor
init(const char * path)45 Return<Result> HdmiCecPort::init(const char* path) {
46     mCecFd = open(path, O_RDWR);
47     if (mCecFd < 0) {
48         LOG(ERROR) << "Failed to open " << path << ", Error = " << strerror(errno);
49         return Result::FAILURE_NOT_SUPPORTED;
50     }
51     mExitFd = eventfd(0, EFD_NONBLOCK);
52     if (mExitFd < 0) {
53         LOG(ERROR) << "Failed to open eventfd, Error = " << strerror(errno);
54         release();
55         return Result::FAILURE_NOT_SUPPORTED;
56     }
57 
58     // Ensure the CEC device supports required capabilities
59     struct cec_caps caps = {};
60     int ret = ioctl(mCecFd, CEC_ADAP_G_CAPS, &caps);
61     if (ret) {
62         LOG(ERROR) << "Unable to query cec adapter capabilities, Error = " << strerror(errno);
63         release();
64         return Result::FAILURE_NOT_SUPPORTED;
65     }
66 
67     if (!(caps.capabilities & (CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | CEC_CAP_PASSTHROUGH))) {
68         LOG(ERROR) << "Wrong cec adapter capabilities " << caps.capabilities;
69         release();
70         return Result::FAILURE_NOT_SUPPORTED;
71     }
72 
73     uint32_t mode = CEC_MODE_INITIATOR | CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
74     ret = ioctl(mCecFd, CEC_S_MODE, &mode);
75     if (ret) {
76         LOG(ERROR) << "Unable to set initiator mode, Error = " << strerror(errno);
77         release();
78         return Result::FAILURE_NOT_SUPPORTED;
79     }
80     return Result::SUCCESS;
81 }
82 
release()83 Return<void> HdmiCecPort::release() {
84     if (mExitFd > 0) {
85         uint64_t tmp = 1;
86         write(mExitFd, &tmp, sizeof(tmp));
87     }
88     if (mExitFd > 0) {
89         close(mExitFd);
90     }
91     if (mCecFd > 0) {
92         close(mCecFd);
93     }
94     return Void();
95 }
96 }  // namespace implementation
97 }  // namespace V1_0
98 }  // namespace cec
99 }  // namespace tv
100 }  // namespace hardware
101 }  // namespace android
102