From 055661789d74a2b43517b4c6e3acb7bc5ff14ad9 Mon Sep 17 00:00:00 2001
From: hmz007 <hmz007@gmail.com>
Date: Thu, 11 May 2023 17:47:01 +0800
Subject: [PATCH] rockchip: minor fixes for hdmi_cec device module

Signed-off-by: hmz007 <hmz007@gmail.com>
---
 hardware/rockchip/hdmicec/hdmi_cec.cpp      | 10 +++++-----
 hardware/rockchip/hdmicec/hdmicec_event.cpp | 22 +++++++++++----------
 2 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/hardware/rockchip/hdmicec/hdmi_cec.cpp b/hardware/rockchip/hdmicec/hdmi_cec.cpp
index 24ca41b31ba..6c1aed4a8b6 100644
--- a/hardware/rockchip/hdmicec/hdmi_cec.cpp
+++ b/hardware/rockchip/hdmicec/hdmi_cec.cpp
@@ -483,14 +483,14 @@ static int hdmi_cec_device_open(const struct hw_module_t* module, const char* na
 	dev->device.set_audio_return_channel = hdmi_cec_set_audio_return_channel;
 	dev->device.is_connected = hdmi_cec_is_connected;
 	dev->phy_addr = 0;
+
 	dev->fd = open(HDMI_DEV_PATH,O_RDWR,0);
-	ALOGE(HDMI_DEV_PATH);
-	ALOGE("\n");
 	if (dev->fd < 0) {
-		ALOGE("%s open error!", __func__);
-		ALOGE("cec %s\n", strerror(errno));
+		ALOGE("%s failed to open %s, %d", __func__, HDMI_DEV_PATH, errno);
+	} else {
+		ALOGI("%s dev->fd = %d", __func__, dev->fd);
 	}
-	ALOGI("%s dev->fd = %d", __func__, dev->fd);
+
 	property_set("vendor.sys.hdmicec.version",HDMI_CEC_HAL_VERSION);
 	*device = &dev->device.common;
 	init_uevent_thread(dev);
diff --git a/hardware/rockchip/hdmicec/hdmicec_event.cpp b/hardware/rockchip/hdmicec/hdmicec_event.cpp
index e76503c841a..56e7b3021a7 100644
--- a/hardware/rockchip/hdmicec/hdmicec_event.cpp
+++ b/hardware/rockchip/hdmicec/hdmicec_event.cpp
@@ -32,6 +32,7 @@
 #include <hdmicec.h>
 #include <unistd.h>
 #include <pthread.h>
+#include <cutils/properties.h>
 
 #define HDMI_CEC_UEVENT_THREAD_NAME "HdmiCecThread"
 
@@ -102,13 +103,14 @@ static void *uevent_loop(void *param)
 	struct pollfd pfd[2];
 	int fd[2];
 	int ret, i;
+	bool cec_debug = property_get_bool("vendor.sys.hdmicec.debug", false);
 
 	prctl(PR_SET_NAME, (unsigned long) &thread_name, 0, 0, 0);
 	setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
 
 	fd[0] = ctx->fd;
 	if (fd[0] < 0) {
-		ALOGE ("%s:not able to open cec state node", __func__);
+		ALOGE ("%s: unable to open cec state node", __func__);
 		return NULL;
 	}
 
@@ -117,20 +119,20 @@ static void *uevent_loop(void *param)
 		pfd[0].events = POLLIN | POLLRDNORM | POLLPRI;
 
 	while (true) {
-                usleep(1000);
-		int err = poll(&pfd[0], 1, 20);
+		// usleep(1000);
+		int err = poll(&pfd[0], 1, -1);
 
 		if (!err) {
 			continue;
-		} else if(err > 0) {
+		} else if (err > 0) {
 			if (!ctx->enable || !ctx->system_control)
 				continue;
-			ALOGD("poll revent:%02x\n", pfd[0].revents);
+			if (cec_debug) ALOGD("poll revent:%02x\n", pfd[0].revents);
 			memset(&cec_event, 0, sizeof(hdmi_event_t));
 			if (pfd[0].revents & (POLLIN)) {
 				struct cec_msg cecframe;
 
-				ALOGD("poll receive msg\n");
+				if (cec_debug) ALOGD("poll receive msg\n");
 				ret = ioctl(pfd[0].fd, CEC_RECEIVE, &cecframe);
 				if (!ret) {
 					cec_event.type = HDMI_EVENT_CEC_MESSAGE;
@@ -142,8 +144,9 @@ static void *uevent_loop(void *param)
 					if (!validcecmessage(cec_event)) {
 						for (ret = 0; ret < cec_event.cec.length; ret++)
 						     cec_event.cec.body [ret + 1] = cecframe.msg[ret + 2];
-						for (i = 0; i < cecframe.len; i++)
-							ALOGD("poll receive msg[%d]:%02x\n", i, cecframe.msg[i]);
+						for (i = 0; i < cecframe.len; i++) {
+							if (cec_debug) ALOGD("poll receive msg[%d]:%02x\n", i, cecframe.msg[i]);
+						}
 						if (ctx->event_callback)
 							ctx->event_callback(&cec_event, ctx->cec_arg);
 					} else {
@@ -213,8 +216,7 @@ static void *uevent_loop(void *param)
 				}
 			}
 		} else {
-			ALOGE("%s: cec poll failed errno: %s", __FUNCTION__,
-			strerror(errno));
+			ALOGE("%s: cec poll failed errno: %s", __FUNCTION__, strerror(errno));
 			continue;
 		}
 	}