hal: Audio elite make card_state path compatible for 5.4/5.15
* Kernel 5.4 card_state will be located at /proc/asound/card%d/state
The state is OFFLINE/ONLINE
* Kernel 5.15 card_state will be located at /sys/kernel/snd_card/card_state
The state is 0/1
* Select different path and read the state based on kernel version
Change-Id: Idfed6f2097e32ccbd571bf6e04419a64dfa024ad
diff --git a/hal/audio_extn/sndmonitor.c b/hal/audio_extn/sndmonitor.c
index 5e39d37..a0bd6d3 100644
--- a/hal/audio_extn/sndmonitor.c
+++ b/hal/audio_extn/sndmonitor.c
@@ -55,6 +55,7 @@
#include <log/log.h>
#include <cutils/str_parms.h>
#include <ctype.h>
+#include <linux/version.h>
#include "audio_hw.h"
#include "audio_extn.h"
@@ -158,17 +159,20 @@
s->fd = fd; // dup?
char *state = read_state(fd);
-
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 0))
if (!state) {
free(s);
return -1;
}
bool online = state && !strcmp(state, "ONLINE");
- ALOGV("card %d initial state %s %d", card, state, online);
-
if (state)
free(state);
+#else
+ bool online = atoi(state);
+#endif
+
+ ALOGV("card %d initial state %s %d", card, state, online);
s->status = online ? CARD_STATUS_ONLINE : CARD_STATUS_OFFLINE;
list_add_tail(&sndmonitor.cards, &s->node);
@@ -234,8 +238,11 @@
ALOGW("Skip over non-ADSP snd card %s", card_id);
continue;
}
-
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 0))
snprintf(path, sizeof(path), "/proc/asound/card%s/state", ptr);
+#else
+ snprintf(path, sizeof(path), "/sys/kernel/snd_card/card_state");
+#endif
ALOGV("Opening sound card state : %s", path);
fd = open(path, O_RDONLY);
@@ -446,18 +453,26 @@
char rd_buf[9]={0};
card_status_t status;
- if (read(s->fd, rd_buf, 8) <= 0)
+ if (read(s->fd, rd_buf, 8) < 0) {
+ ALOGE("read card state error");
return -1;
+ }
rd_buf[8] = '\0';
lseek(s->fd, 0, SEEK_SET);
ALOGV("card num %d, new state %s", s->card, rd_buf);
-
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 0))
if (strstr(rd_buf, "OFFLINE"))
status = CARD_STATUS_OFFLINE;
else if (strstr(rd_buf, "ONLINE"))
status = CARD_STATUS_ONLINE;
+#else
+ if (!atoi(rd_buf))
+ status = CARD_STATUS_OFFLINE;
+ else if (atoi(rd_buf))
+ status = CARD_STATUS_ONLINE;
+#endif
else {
ALOGE("unknown state");
return 0;