dragonboards: sync up vendor userspace daemons to upstream sources am: 53c1287103

Original change: https://android-review.googlesource.com/c/device/linaro/dragonboard/+/2300537

Change-Id: Ie6bac23779e85af4cc28bbe332e0e76c27aa83b2
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/qcom/pd-mapper/pd-mapper.c b/qcom/pd-mapper/pd-mapper.c
index 664b77d..376d9fe 100644
--- a/qcom/pd-mapper/pd-mapper.c
+++ b/qcom/pd-mapper/pd-mapper.c
@@ -36,6 +36,7 @@
 #include <fcntl.h>
 #include <libgen.h>
 #include <libqrtr.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
diff --git a/qcom/qrtr/Makefile b/qcom/qrtr/Makefile
index f814aa2..036691c 100644
--- a/qcom/qrtr/Makefile
+++ b/qcom/qrtr/Makefile
@@ -126,7 +126,7 @@
 
 $(DESTDIR)$(servicedir)/$1: $1
 	@echo "INSTALL	$$<"
-	@install -D -m 755 $$< $$@
+	@install -D -m 644 $$< $$@
 
 all-install += $(DESTDIR)$(servicedir)/$1
 endef
diff --git a/qcom/qrtr/lib/libqrtr.h b/qcom/qrtr/lib/libqrtr.h
index 87433ed..93254df 100644
--- a/qcom/qrtr/lib/libqrtr.h
+++ b/qcom/qrtr/lib/libqrtr.h
@@ -4,6 +4,7 @@
 #include <linux/qrtr.h>
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <stddef.h>
 #include <stdint.h>
 
 #ifdef __cplusplus
@@ -11,7 +12,7 @@
 #endif
 
 #ifndef offsetof
-#define offsetof(type, md) ((unsigned long)&((type *)0)->md)
+#define offsetof(type, md) ((size_t)&((type *)0)->md)
 #endif
 
 #ifndef container_of
@@ -90,7 +91,7 @@
 	uint32_t elem_size;
 	enum qmi_array_type array_type;
 	uint8_t tlv_type;
-	uint32_t offset;
+	size_t offset;
 	struct qmi_elem_info *ei_array;
 };
 
diff --git a/qcom/qrtr/lib/qmi.c b/qcom/qrtr/lib/qmi.c
index f1c0293..d217a2d 100644
--- a/qcom/qrtr/lib/qmi.c
+++ b/qcom/qrtr/lib/qmi.c
@@ -249,8 +249,8 @@
 			LOGW("%s: STRUCT Encode failure\n", __func__);
 			return rc;
 		}
-		buf_dst = (char*)buf_dst + rc;
-		buf_src = (char*)buf_src + temp_ei->elem_size;
+		buf_dst = (void*)((char*)buf_dst + rc);
+		buf_src = (void*)((char*)buf_src + temp_ei->elem_size);
 		encoded_bytes += rc;
 	}
 
@@ -310,7 +310,7 @@
 		encoded_bytes += rc;
 	}
 
-	rc = qmi_encode_basic_elem((char*)buf_dst + encoded_bytes, buf_src,
+	rc = qmi_encode_basic_elem((void*)((char*)buf_dst + encoded_bytes), buf_src,
 				   string_len, temp_ei->elem_size);
 	encoded_bytes += rc;
 
@@ -354,7 +354,7 @@
 		buf_dst = buf_dst + (TLV_LEN_SIZE + TLV_TYPE_SIZE);
 
 	while (temp_ei->data_type != QMI_EOTI) {
-		buf_src = (char*)in_c_struct + temp_ei->offset;
+		buf_src = (void*)((char*)in_c_struct + temp_ei->offset);
 		tlv_type = temp_ei->tlv_type;
 
 		if (temp_ei->array_type == NO_ARRAY) {
@@ -522,8 +522,8 @@
 				tlv_len - decoded_bytes, dec_level);
 		if (rc < 0)
 			return rc;
-		buf_src = (char*)buf_src + rc;
-		buf_dst = (char*)buf_dst + temp_ei->elem_size;
+		buf_src = (void*)((char*)buf_src + rc);
+		buf_dst = (void*)((char*)buf_dst + temp_ei->elem_size);
 		decoded_bytes += rc;
 	}
 
@@ -585,7 +585,7 @@
 		return -EFAULT;
 	}
 
-	rc = qmi_decode_basic_elem(buf_dst, (char*)buf_src + decoded_bytes,
+	rc = qmi_decode_basic_elem(buf_dst, (void*)((char*)buf_src + decoded_bytes),
 				   string_len, temp_ei->elem_size);
 	*((char *)buf_dst + string_len) = '\0';
 	decoded_bytes += rc;
@@ -654,7 +654,7 @@
 			tlv_pointer = buf_src;
 			QMI_ENCDEC_DECODE_TLV(&tlv_type,
 					      &tlv_len, tlv_pointer);
-			buf_src = (uint8_t*)buf_src + (TLV_TYPE_SIZE + TLV_LEN_SIZE);
+			buf_src = (void*)((char*)buf_src + (TLV_TYPE_SIZE + TLV_LEN_SIZE));
 			decoded_bytes += (TLV_TYPE_SIZE + TLV_LEN_SIZE);
 			temp_ei = find_ei(ei_array, tlv_type);
 			if (!temp_ei && tlv_type < OPTIONAL_TLV_TYPE_START) {
@@ -673,11 +673,11 @@
 			tlv_len = in_buf_len - decoded_bytes;
 		}
 
-		buf_dst = (uint8_t*)out_c_struct + temp_ei->offset;
+		buf_dst = (void*)((char*)out_c_struct + temp_ei->offset);
 		if (temp_ei->data_type == QMI_OPT_FLAG) {
 			memcpy(buf_dst, &opt_flag_value, sizeof(uint8_t));
 			temp_ei = temp_ei + 1;
-			buf_dst = (uint8_t*)out_c_struct + temp_ei->offset;
+			buf_dst = (void*)((char*)out_c_struct + temp_ei->offset);
 		}
 
 		if (temp_ei->data_type == QMI_DATA_LEN) {
@@ -687,7 +687,7 @@
 						   1, data_len_sz);
 			memcpy(buf_dst, &data_len_value, sizeof(uint32_t));
 			temp_ei = temp_ei + 1;
-			buf_dst = (uint8_t*)out_c_struct + temp_ei->offset;
+			buf_dst = (void*)((char*)out_c_struct + temp_ei->offset);
 			tlv_len -= data_len_sz;
 			UPDATE_DECODE_VARIABLES(buf_src, decoded_bytes, rc);
 		}
@@ -777,7 +777,7 @@
 
 	/* Encode message, if we have a message */
 	if (c_struct) {
-		msglen = qmi_encode(ei, (char*)pkt->data + sizeof(*hdr), c_struct,
+		msglen = qmi_encode(ei, (void*)((char*)pkt->data + sizeof(*hdr)), c_struct,
 				    pkt->data_len - sizeof(*hdr), 1);
 		if (msglen < 0)
 			return msglen;
@@ -839,7 +839,7 @@
 	if (txn)
 		*txn = hdr->txn_id;
 
-	return qmi_decode(ei, c_struct, (char*)pkt->data + sizeof(*hdr), pkt->data_len - sizeof(*hdr), 1);
+	return qmi_decode(ei, c_struct, (void*)((char*)pkt->data + sizeof(*hdr)), pkt->data_len - sizeof(*hdr), 1);
 }
 
 /* Common header in all QMI responses */
diff --git a/qcom/qrtr/lib/qrtr.c b/qcom/qrtr/lib/qrtr.c
index 7c1c389..96756ba 100644
--- a/qcom/qrtr/lib/qrtr.c
+++ b/qcom/qrtr/lib/qrtr.c
@@ -52,7 +52,7 @@
 	}
 
 	if (rport != 0) {
-		struct sockaddr_qrtr sq;
+		struct sockaddr_qrtr sq = {};
 
 		sq.sq_family = AF_QIPCRTR;
 		sq.sq_node = 1;
@@ -78,7 +78,7 @@
 
 int qrtr_sendto(int sock, uint32_t node, uint32_t port, const void *data, unsigned int sz)
 {
-	struct sockaddr_qrtr sq;
+	struct sockaddr_qrtr sq = {};
 	int rc;
 
 	sq.sq_family = AF_QIPCRTR;
diff --git a/qcom/qrtr/src/list.h b/qcom/qrtr/src/list.h
index d740743..1d1c8e6 100644
--- a/qcom/qrtr/src/list.h
+++ b/qcom/qrtr/src/list.h
@@ -1,8 +1,10 @@
 #ifndef _LIST_H_
 #define _LIST_H_
 
+#include <stddef.h>
+
 #ifndef offsetof
-#define offsetof(type, md) ((unsigned long)&((type *)0)->md)
+#define offsetof(type, md) ((size_t)&((type *)0)->md)
 #endif
 
 #ifndef container_of
diff --git a/qcom/qrtr/src/lookup.c b/qcom/qrtr/src/lookup.c
index 3312e40..80cf984 100644
--- a/qcom/qrtr/src/lookup.c
+++ b/qcom/qrtr/src/lookup.c
@@ -64,6 +64,7 @@
 	{ 41, 0, "RF radiated performance enhancement service" },
 	{ 42, 0, "Data system determination service" },
 	{ 43, 0, "Subsystem control service" },
+	{ 47, 0, "Data Port Mapper service" },
 	{ 49, 0, "IPA control service" },
 	{ 51, 0, "CoreSight remote tracing service" },
 	{ 52, 0, "Dynamic Heap Memory Sharing" },
@@ -76,6 +77,7 @@
 	{ 312, 0, "QBT1000 Ultrasonic Fingerprint Sensor service" },
 	{ 769, 0, "SLIMbus control service" },
 	{ 771, 0, "Peripheral Access Control Manager service" },
+	{ 4096, 0, "TFTP" },
 	{ DIAG_SERVICE, 0, "DIAG service" },
 };
 
diff --git a/qcom/rmtfs/rmtfs.c b/qcom/rmtfs/rmtfs.c
index 93965f1..b3ed289 100644
--- a/qcom/rmtfs/rmtfs.c
+++ b/qcom/rmtfs/rmtfs.c
@@ -220,6 +220,10 @@
 respond:
 	dbgprintf("[RMTFS] iovec %d, %sforced => (%d:%d)\n", caller_id, force ? "" : "not ",
 							     resp.result.result, resp.result.error);
+
+	if (is_write)
+		storage_sync(rmtfd);
+
 	for (i = 0; i < num_entries; i++) {
 		dbgprintf("[RMTFS]       %s %d:%d 0x%x\n", is_write ? "write" : "read",
 							   entries[i].sector_addr,
@@ -445,7 +449,10 @@
 		rproc_start();
 
 	for (;;) {
-		if (rprocfd >= 0 && sig_int_count == 1 && !sig_int_handled) {
+		if (sig_int_count == 1 && !sig_int_handled) {
+			if (rprocfd < 0)
+				break;
+
 			rproc_stop();
 			sig_int_handled = true;
 		} else if (sig_int_count > 1) {
diff --git a/qcom/rmtfs/rmtfs.h b/qcom/rmtfs/rmtfs.h
index 242baa5..fa4b806 100644
--- a/qcom/rmtfs/rmtfs.h
+++ b/qcom/rmtfs/rmtfs.h
@@ -34,6 +34,7 @@
 void storage_exit(void);
 ssize_t storage_pread(const struct rmtfd *rmtfd, void *buf, size_t nbyte, off_t offset);
 ssize_t storage_pwrite(struct rmtfd *rmtfd, const void *buf, size_t nbyte, off_t offset);
+int storage_sync(struct rmtfd *rmtfd);
 
 int rproc_init(void);
 int rproc_start(void);
diff --git a/qcom/rmtfs/rproc.c b/qcom/rmtfs/rproc.c
index 95b45cb..68dc3c9 100644
--- a/qcom/rmtfs/rproc.c
+++ b/qcom/rmtfs/rproc.c
@@ -13,13 +13,75 @@
 #include "rmtfs.h"
 
 #define RPROC_BASE_PATH		"/sys/bus/platform/drivers/qcom-q6v5-mss/"
+#define RPROC_CLASS_PATH	"/sys/class/remoteproc/"
 
 static pthread_t start_thread;
 static pthread_t stop_thread;
 static int rproc_state_fd;
 static int rproc_pipe[2];
 
-int rproc_init(void)
+static int rproc_init_by_modalias(void)
+{
+	struct dirent *rproc_de;
+	char modalias[256];
+	DIR *base_dir;
+	int modalias_fd;
+	int rproc_fd;
+	int state_fd = -1;
+	int base_fd;
+	int ret;
+
+	base_fd = open(RPROC_CLASS_PATH, O_RDONLY | O_DIRECTORY);
+	if (base_fd < 0)
+		return -1;
+
+	base_dir = fdopendir(base_fd);
+	if (!base_dir) {
+		fprintf(stderr, "failed to open remoteproc class path\n");
+		close(base_fd);
+		return -1;
+	}
+
+	while (state_fd < 0 && (rproc_de = readdir(base_dir)) != NULL) {
+		if (!strcmp(rproc_de->d_name, ".") ||
+		    !strcmp(rproc_de->d_name, ".."))
+			continue;
+
+		rproc_fd = openat(base_fd, rproc_de->d_name, O_RDONLY | O_DIRECTORY);
+		if (rproc_fd < 0)
+			continue;
+
+		modalias_fd = openat(rproc_fd, "device/modalias", O_RDONLY);
+		if (modalias_fd < 0)
+			goto close_rproc_fd;
+
+		ret = read(modalias_fd, modalias, sizeof(modalias) - 1);
+		if (ret < 0)
+			goto close_modalias_fd;
+		modalias[ret] = '\0';
+
+		if (!strstr(modalias, "-mpss-pas") && !strstr(modalias, "-mss-pil"))
+			goto close_modalias_fd;
+
+		state_fd = openat(rproc_fd, "state", O_WRONLY);
+		if (state_fd < 0) {
+			fprintf(stderr,
+				"unable to open remoteproc \"state\" control file of %s\n",
+				rproc_de->d_name);
+		}
+
+close_modalias_fd:
+		close(modalias_fd);
+close_rproc_fd:
+		close(rproc_fd);
+	}
+	closedir(base_dir);
+	close(base_fd);
+
+	return state_fd;
+}
+
+static int rproc_init_by_mss_driver(void)
 {
 	struct dirent *device_de;
 	struct dirent *rproc_de;
@@ -28,10 +90,8 @@
 	DIR *base_dir;
 	int device_fd;
 	int rproc_fd;
+	int state_fd = -1;
 	int base_fd;
-	int ret;
-
-	rproc_state_fd = -1;
 
 	base_fd = open(RPROC_BASE_PATH, O_RDONLY | O_DIRECTORY);
 	if (base_fd < 0)
@@ -44,7 +104,7 @@
 		return -1;
 	}
 
-	while (rproc_state_fd < 0 && (device_de = readdir(base_dir)) != NULL) {
+	while (state_fd < 0 && (device_de = readdir(base_dir)) != NULL) {
 		if (!strcmp(device_de->d_name, ".") ||
 		    !strcmp(device_de->d_name, ".."))
 			continue;
@@ -60,7 +120,7 @@
 		}
 
 		rproc_dir = fdopendir(rproc_base_fd);
-		while (rproc_state_fd < 0 && (rproc_de = readdir(rproc_dir)) != NULL) {
+		while (state_fd < 0 && (rproc_de = readdir(rproc_dir)) != NULL) {
 			if (!strcmp(rproc_de->d_name, ".") ||
 			    !strcmp(rproc_de->d_name, ".."))
 				continue;
@@ -69,8 +129,8 @@
 			if (rproc_fd < 0)
 				continue;
 
-			rproc_state_fd = openat(rproc_fd, "state", O_WRONLY);
-			if (rproc_state_fd < 0) {
+			state_fd = openat(rproc_fd, "state", O_WRONLY);
+			if (state_fd < 0) {
 				fprintf(stderr,
 					"unable to open remoteproc \"state\" control file of %s\n",
 					device_de->d_name);
@@ -86,15 +146,29 @@
 	closedir(base_dir);
 	close(base_fd);
 
-	if (rproc_state_fd < 0)
-		return -1;
+	return state_fd;
+}
+
+int rproc_init(void)
+{
+	int state_fd;
+	int ret;
+
+	state_fd = rproc_init_by_modalias();
+	if (state_fd < 0) {
+		state_fd = rproc_init_by_mss_driver();
+		if (state_fd < 0)
+			return -1;
+	}
 
 	ret = pipe(rproc_pipe);
 	if (ret < 0) {
-		close(rproc_state_fd);
+		close(state_fd);
 		return -1;
 	}
 
+	rproc_state_fd = state_fd;
+
 	return rproc_pipe[0];
 }
 
@@ -103,15 +177,22 @@
 	ssize_t ret;
 
 	ret = pwrite(rproc_state_fd, "start", 5, 0);
-	if (ret < 4)
-		fprintf(stderr, "failed to update start state\n");
+	if (ret < 4) {
+		fprintf(stderr, "failed to update start state: %s\n",
+			strerror(errno));
+	}
 
 	return NULL;
 }
 
 int rproc_start()
 {
-	return pthread_create(&start_thread, NULL, do_rproc_start, NULL);
+	pthread_attr_t attr;
+
+	pthread_attr_init(&attr);
+	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+
+	return pthread_create(&start_thread, &attr, do_rproc_start, NULL);
 }
 
 static void *do_rproc_stop(void *unused __unused)
@@ -119,8 +200,10 @@
 	ssize_t ret;
 
 	ret = pwrite(rproc_state_fd, "stop", 4, 0);
-	if (ret < 4)
-		fprintf(stderr, "failed to update stop state\n");
+	if (ret < 4) {
+		fprintf(stderr, "failed to update stop state: %s\n",
+			strerror(errno));
+	}
 
 	ret = write(rproc_pipe[1], "Y", 1);
 	if (ret != 1) {
@@ -133,5 +216,10 @@
 
 int rproc_stop(void)
 {
-	return pthread_create(&stop_thread, NULL, do_rproc_stop, NULL);
+	pthread_attr_t attr;
+
+	pthread_attr_init(&attr);
+	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+
+	return pthread_create(&stop_thread, &attr, do_rproc_stop, NULL);
 }
diff --git a/qcom/rmtfs/storage.c b/qcom/rmtfs/storage.c
index c8e69ed..107b296 100644
--- a/qcom/rmtfs/storage.c
+++ b/qcom/rmtfs/storage.c
@@ -41,6 +41,7 @@
 	{ "/boot/modem_fs2", "modem_fs2", "modemst2" },
 	{ "/boot/modem_fsc", "modem_fsc", "fsc" },
 	{ "/boot/modem_fsg", "modem_fsg", "fsg" },
+	{ "/boot/modem_tunning", "modem_tunning", "tunning" },
 	{}
 };
 
@@ -150,8 +151,10 @@
 
 void storage_close(struct rmtfd *rmtfd)
 {
-	close(rmtfd->fd);
-	rmtfd->fd = -1;
+	if (rmtfd->fd >= 0) {
+		close(rmtfd->fd);
+		rmtfd->fd = -1;
+	}
 
 	free(rmtfd->shadow_buf);
 	rmtfd->shadow_buf = NULL;
@@ -188,10 +191,8 @@
 {
 	int i;
 
-	for (i = 0; i < MAX_CALLERS; i++) {
-		if (rmtfds[i].fd >= 0)
-			close(rmtfds[i].fd);
-	}
+	for (i = 0; i < MAX_CALLERS; i++)
+		storage_close(&rmtfds[i]);
 }
 
 ssize_t storage_pread(const struct rmtfd *rmtfd, void *buf, size_t nbyte, off_t offset)
@@ -201,7 +202,7 @@
 	if (!storage_read_only) {
 		n = pread(rmtfd->fd, buf, nbyte, offset);
 	} else {
-		n = MIN(nbyte, rmtfd->shadow_len - offset);
+		n = MIN((ssize_t)nbyte, (ssize_t)rmtfd->shadow_len - offset);
 		if (n > 0)
 			memcpy(buf, (char*)rmtfd->shadow_buf + offset, n);
 		else
@@ -244,6 +245,14 @@
 	return nbyte;
 }
 
+int storage_sync(struct rmtfd *rmtfd)
+{
+	if (storage_read_only)
+		return 0;
+	
+	return fdatasync(rmtfd->fd);
+}
+
 static int storage_populate_shadow_buf(struct rmtfd *rmtfd, const char *file)
 {
 	ssize_t len;