Skip to contentSkip to conversation

qmi: Remove qmi_device_sync

PATCH Superseded Denis Kenzior 1 participant 7 messages Feb 20, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/7 qmi: Remove qmi_device_sync

This is an implementation detail of the QMUX/CTL interface. Since the only caller always invokes SYNC after discovery if supported, simplify the SYNC operation to be part of the discovery procedure and remove the relevant APIs from qmi_device.

---
 drivers/qmimodem/qmi.c | 88 +++++++++++++++++-------------------------
 drivers/qmimodem/qmi.h |  5 ---
 plugins/gobi.c         | 10 ++---
 3 files changed, 39 insertions(+), 64 deletions(-)
drivers/qmimodem/qmi.c +36 -52
index c9c809757427..ee51a7db2368 100644
@@ -1176,6 +1176,35 @@ static void discover_data_free(gpointer user_data)
 	g_free(data);
 }
 
+static void qmi_device_sync_callback(uint16_t message, uint16_t length,
+					const void *buffer, void *user_data)
+{
+	struct discover_data *data = user_data;
+
+	if (data->func)
+		data->func(data->user_data);
+
+	__qmi_device_discovery_complete(data->device, &data->super);
+}
+
+/* sync will release all previous clients */
+static bool qmi_device_sync(struct qmi_device *device,
+				struct discover_data *data)
+{
+	struct qmi_request *req;
+
+	__debug_device(device, "Sending sync to reset QMI");
+
+	req = __request_alloc(QMI_SERVICE_CONTROL, 0x00,
+				QMI_CTL_SYNC, NULL, 0,
+				qmi_device_sync_callback, data);
+
+	__request_submit(device, req);
+
+	return true;
+}
+
+
 static void discover_callback(uint16_t message, uint16_t length,
 					const void *buffer, void *user_data)
 {
@@ -1249,6 +1278,13 @@ done:
 	device->version_list = list;
 	device->version_count = count;
 
+	/* if the device support the QMI call SYNC over the CTL interface */
+	if ((device->control_major == 1 && device->control_minor >= 5) ||
+			device->control_major > 1) {
+		qmi_device_sync(data->device, data);
+		return;
+	}
+
 	if (data->func)
 		data->func(data->user_data);
 
@@ -1414,58 +1450,6 @@ bool qmi_device_shutdown(struct qmi_device *device, qmi_shutdown_func_t func,
 	return true;
 }
 
-struct sync_data {
-	qmi_sync_func_t func;
-	void *user_data;
-};
-
-static void qmi_device_sync_callback(uint16_t message, uint16_t length,
-				     const void *buffer, void *user_data)
-{
-	struct sync_data *data = user_data;
-
-	if (data->func)
-		data->func(data->user_data);
-
-	g_free(data);
-}
-
-/* sync will release all previous clients */
-bool qmi_device_sync(struct qmi_device *device,
-		     qmi_sync_func_t func, void *user_data)
-{
-	struct qmi_request *req;
-	struct sync_data *func_data;
-
-	if (!device)
-		return false;
-
-	__debug_device(device, "Sending sync to reset QMI");
-
-	func_data = g_new0(struct sync_data, 1);
-	func_data->func = func;
-	func_data->user_data = user_data;
-
-	req = __request_alloc(QMI_SERVICE_CONTROL, 0x00,
-			QMI_CTL_SYNC,
-			NULL, 0,
-			qmi_device_sync_callback, func_data);
-
-	__request_submit(device, req);
-
-	return true;
-}
-
-/* if the device support the QMI call SYNC over the CTL interface */
-bool qmi_device_is_sync_supported(struct qmi_device *device)
-{
-	if (device == NULL)
-		return false;
-
-	return (device->control_major > 1 ||
-		(device->control_major == 1 && device->control_minor >= 5));
-}
-
 static bool get_device_file_name(struct qmi_device *device,
 					char *file_name, int size)
 {
drivers/qmimodem/qmi.h +0 -5
index 21537a76c64e..fc2a309578c4 100644
@@ -78,7 +78,6 @@ typedef void (*qmi_destroy_func_t)(void *user_data);
 struct qmi_device;
 
 typedef void (*qmi_debug_func_t)(const char *str, void *user_data);
-typedef void (*qmi_sync_func_t)(void *user_data);
 typedef void (*qmi_shutdown_func_t)(void *user_data);
 typedef void (*qmi_discover_func_t)(void *user_data);
 
@@ -101,10 +100,6 @@ bool qmi_device_has_service(struct qmi_device *device, uint16_t type);
 bool qmi_device_get_service_version(struct qmi_device *device, uint16_t type,
 					uint16_t *major, uint16_t *minor);
 
-bool qmi_device_sync(struct qmi_device *device,
-		     qmi_sync_func_t func, void *user_data);
-bool qmi_device_is_sync_supported(struct qmi_device *device);
-
 enum qmi_device_expected_data_format qmi_device_get_expected_data_format(
 						struct qmi_device *device);
 bool qmi_device_set_expected_data_format(struct qmi_device *device,
plugins/gobi.c +3 -7
index 76c9d68d2df5..020d6ba2685e 100644
@@ -364,13 +364,12 @@ error:
 	shutdown_device(modem);
 }
 
-static void create_shared_dms(void *user_data)
+static void create_shared_dms(struct ofono_modem *modem)
 {
-	struct ofono_modem *modem = user_data;
 	struct gobi_data *data = ofono_modem_get_data(modem);
 
 	qmi_service_create_shared(data->device, QMI_SERVICE_DMS,
-				  create_dms_cb, modem, NULL);
+						create_dms_cb, modem, NULL);
 }
 
 static void discover_cb(void *user_data)
@@ -417,10 +416,7 @@ static void discover_cb(void *user_data)
 		return;
 	}
 
-	if (qmi_device_is_sync_supported(data->device))
-		qmi_device_sync(data->device, create_shared_dms, modem);
-	else
-		create_shared_dms(modem);
+	create_shared_dms(modem);
 }
 
 static int gobi_enable(struct ofono_modem *modem)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/7 qmi: Introduce qmi_device_new_qmux

In preparation for QRTR support, remove the existing qmi_device_new API and rename it to qmi_device_new_qmux. Since QMUX usually runs on top of a character device, have the constructor take in the character device path, instead of having the caller open it manually.

As part of this refactoring, introduce the initial qmi_device_ops structure which will be used to abstract qmi operation over QMUX and QRTR.

---
 drivers/qmimodem/qmi.c | 70 ++++++++++++++++++++++++++++++++----------
 drivers/qmimodem/qmi.h |  4 +--
 plugins/gobi.c         | 11 +------
 3 files changed, 57 insertions(+), 28 deletions(-)
drivers/qmimodem/qmi.c +54 -16
index ee51a7db2368..89e1f8f40381 100644
@@ -34,6 +34,7 @@
 #include <string.h>
 
 #include <glib.h>
+#include <ell/ell.h>
 
 #include <ofono/log.h>
 
@@ -54,6 +55,10 @@ struct qmi_version {
 	const char *name;
 };
 
+struct qmi_device_ops {
+	void (*destroy)(struct qmi_device *device);
+};
+
 struct qmi_device {
 	int ref_count;
 	int fd;
@@ -80,10 +85,15 @@ struct qmi_device {
 	void *shutdown_user_data;
 	qmi_destroy_func_t shutdown_destroy;
 	guint shutdown_source;
+	const struct qmi_device_ops *ops;
 	bool shutting_down : 1;
 	bool destroyed : 1;
 };
 
+struct qmi_device_qmux {
+	struct qmi_device super;
+};
+
 struct qmi_service {
 	int ref_count;
 	struct qmi_device *device;
@@ -949,15 +959,11 @@ static void service_destroy(gpointer data)
 	service->device = NULL;
 }
 
-struct qmi_device *qmi_device_new(int fd)
+static int qmi_device_init(struct qmi_device *device, int fd,
+					const struct qmi_device_ops *ops)
 {
-	struct qmi_device *device;
 	long flags;
 
-	device = g_try_new0(struct qmi_device, 1);
-	if (!device)
-		return NULL;
-
 	__debug_device(device, "device %p new", device);
 
 	device->ref_count = 1;
@@ -966,16 +972,14 @@ struct qmi_device *qmi_device_new(int fd)
 	device->close_on_unref = false;
 
 	flags = fcntl(device->fd, F_GETFL, NULL);
-	if (flags < 0) {
-		g_free(device);
-		return NULL;
-	}
+	if (flags < 0)
+		return -EIO;
 
 	if (!(flags & O_NONBLOCK)) {
-		if (fcntl(device->fd, F_SETFL, flags | O_NONBLOCK) < 0) {
-			g_free(device);
-			return NULL;
-		}
+		int r = fcntl(device->fd, F_SETFL, flags | O_NONBLOCK);
+
+		if (r < 0)
+			return -errno;
 	}
 
 	device->io = g_io_channel_unix_new(device->fd);
@@ -1000,7 +1004,9 @@ struct qmi_device *qmi_device_new(int fd)
 	device->next_control_tid = 1;
 	device->next_service_tid = 256;
 
-	return device;
+	device->ops = ops;
+
+	return 0;
 }
 
 struct qmi_device *qmi_device_ref(struct qmi_device *device)
@@ -1055,7 +1061,7 @@ void qmi_device_unref(struct qmi_device *device)
 	if (device->shutting_down)
 		device->destroyed = true;
 	else
-		g_free(device);
+		device->ops->destroy(device);
 }
 
 void qmi_device_set_debug(struct qmi_device *device,
@@ -1646,6 +1652,38 @@ done:
 	return res;
 }
 
+static void qmi_device_qmux_destroy(struct qmi_device *device)
+{
+	struct qmi_device_qmux *qmux =
+		l_container_of(device, struct qmi_device_qmux, super);
+
+	l_free(qmux);
+}
+
+static const struct qmi_device_ops qmux_ops = {
+	.destroy = qmi_device_qmux_destroy,
+};
+
+struct qmi_device *qmi_device_new_qmux(const char *device)
+{
+	struct qmi_device_qmux *qmux;
+	int fd;
+
+	fd = open(device, O_RDWR | O_NONBLOCK | O_CLOEXEC);
+	if (fd < 0)
+		return NULL;
+
+	qmux = l_new(struct qmi_device_qmux, 1);
+
+	if (qmi_device_init(&qmux->super, fd, &qmux_ops) < 0) {
+		close(fd);
+		l_free(qmux);
+		return NULL;
+	}
+
+	return &qmux->super;
+}
+
 struct qmi_param *qmi_param_new(void)
 {
 	struct qmi_param *param;
drivers/qmimodem/qmi.h +2 -2
index fc2a309578c4..410ada04d8d7 100644
@@ -81,8 +81,6 @@ typedef void (*qmi_debug_func_t)(const char *str, void *user_data);
 typedef void (*qmi_shutdown_func_t)(void *user_data);
 typedef void (*qmi_discover_func_t)(void *user_data);
 
-struct qmi_device *qmi_device_new(int fd);
-
 struct qmi_device *qmi_device_ref(struct qmi_device *device);
 void qmi_device_unref(struct qmi_device *device);
 
@@ -105,6 +103,8 @@ enum qmi_device_expected_data_format qmi_device_get_expected_data_format(
 bool qmi_device_set_expected_data_format(struct qmi_device *device,
 			enum qmi_device_expected_data_format format);
 
+struct qmi_device *qmi_device_new_qmux(const char *device);
+
 struct qmi_param;
 
 struct qmi_param *qmi_param_new(void);
plugins/gobi.c +1 -10
index 020d6ba2685e..07994666ded5 100644
@@ -423,7 +423,6 @@ static int gobi_enable(struct ofono_modem *modem)
 {
 	struct gobi_data *data = ofono_modem_get_data(modem);
 	const char *device;
-	int fd;
 
 	DBG("%p", modem);
 
@@ -431,15 +430,7 @@ static int gobi_enable(struct ofono_modem *modem)
 	if (!device)
 		return -EINVAL;
 
-	fd = open(device, O_RDWR | O_NONBLOCK | O_CLOEXEC);
-	if (fd < 0)
-		return -EIO;
-
-	data->device = qmi_device_new(fd);
-	if (!data->device) {
-		close(fd);
-		return -ENOMEM;
-	}
+	data->device = qmi_device_new_qmux(device);
 
 	if (getenv("OFONO_QMI_DEBUG"))
 		qmi_device_set_debug(data->device, gobi_debug, "QMI: ");
Signature · 1 line
2.43.0
#2 lore raw

PATCH 3/7 qmi: Remove qmi_device_set_close_on_unref

In reply to Denis Kenzior

The only caller of this method always sets close_on_unref to true. Since the underlying file descriptor is no longer exposed, it is safe to remove this method and assume that the file descriptor should always be closed when qmi_device is cleaned up.

---
 drivers/qmimodem/qmi.c | 13 +------------
 drivers/qmimodem/qmi.h |  2 --
 plugins/gobi.c         |  2 --
 3 files changed, 1 insertion(+), 16 deletions(-)
drivers/qmimodem/qmi.c +1 -12
index 89e1f8f40381..8de5170674f8 100644
@@ -63,7 +63,6 @@ struct qmi_device {
 	int ref_count;
 	int fd;
 	GIOChannel *io;
-	bool close_on_unref;
 	guint read_watch;
 	guint write_watch;
 	GQueue *req_queue;
@@ -969,7 +968,6 @@ static int qmi_device_init(struct qmi_device *device, int fd,
 	device->ref_count = 1;
 
 	device->fd = fd;
-	device->close_on_unref = false;
 
 	flags = fcntl(device->fd, F_GETFL, NULL);
 	if (flags < 0)
@@ -1047,8 +1045,7 @@ void qmi_device_unref(struct qmi_device *device)
 	if (device->read_watch > 0)
 		g_source_remove(device->read_watch);
 
-	if (device->close_on_unref)
-		close(device->fd);
+	close(device->fd);
 
 	if (device->shutdown_source)
 		g_source_remove(device->shutdown_source);
@@ -1074,14 +1071,6 @@ void qmi_device_set_debug(struct qmi_device *device,
 	device->debug_data = user_data;
 }
 
-void qmi_device_set_close_on_unref(struct qmi_device *device, bool do_close)
-{
-	if (!device)
-		return;
-
-	device->close_on_unref = do_close;
-}
-
 void qmi_result_print_tlvs(struct qmi_result *result)
 {
 	const void *ptr = result->data;
drivers/qmimodem/qmi.h +0 -2
index 410ada04d8d7..ab0ee479c89d 100644
@@ -87,8 +87,6 @@ void qmi_device_unref(struct qmi_device *device);
 void qmi_device_set_debug(struct qmi_device *device,
 				qmi_debug_func_t func, void *user_data);
 
-void qmi_device_set_close_on_unref(struct qmi_device *device, bool do_close);
-
 bool qmi_device_discover(struct qmi_device *device, qmi_discover_func_t func,
 				void *user_data, qmi_destroy_func_t destroy);
 bool qmi_device_shutdown(struct qmi_device *device, qmi_shutdown_func_t func,
plugins/gobi.c +0 -2
index 07994666ded5..e22b8146cf93 100644
@@ -435,8 +435,6 @@ static int gobi_enable(struct ofono_modem *modem)
 	if (getenv("OFONO_QMI_DEBUG"))
 		qmi_device_set_debug(data->device, gobi_debug, "QMI: ");
 
-	qmi_device_set_close_on_unref(data->device, true);
-
 	qmi_device_discover(data->device, discover_cb, modem, NULL);
 
 	return -EINPROGRESS;
Signature · 1 line
2.43.0
#3 lore raw

PATCH 4/7 qmi: Move control_{major|minor} to qmux sublcass

In reply to Denis Kenzior

Move the following members to the qmux subclass:
- control_major
- control_minor

---
 drivers/qmimodem/qmi.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/qmimodem/qmi.c +8 -6
index 8de5170674f8..dae540251118 100644
@@ -73,8 +73,6 @@ struct qmi_device {
 	uint16_t next_service_tid;
 	qmi_debug_func_t debug_func;
 	void *debug_data;
-	uint16_t control_major;
-	uint16_t control_minor;
 	char *version_str;
 	struct qmi_version *version_list;
 	uint8_t version_count;
@@ -91,6 +89,8 @@ struct qmi_device {
 
 struct qmi_device_qmux {
 	struct qmi_device super;
+	uint16_t control_major;
+	uint16_t control_minor;
 };
 
 struct qmi_service {
@@ -1205,6 +1205,8 @@ static void discover_callback(uint16_t message, uint16_t length,
 {
 	struct discover_data *data = user_data;
 	struct qmi_device *device = data->device;
+	struct qmi_device_qmux *qmux =
+		l_container_of(device, struct qmi_device_qmux, super);
 	const struct qmi_result_code *result_code;
 	const struct qmi_service_list *service_list;
 	const void *ptr;
@@ -1250,8 +1252,8 @@ static void discover_callback(uint16_t message, uint16_t length,
 					type, major, minor);
 
 		if (type == QMI_SERVICE_CONTROL) {
-			device->control_major = major;
-			device->control_minor = minor;
+			qmux->control_major = major;
+			qmux->control_minor = minor;
 			continue;
 		}
 
@@ -1274,8 +1276,8 @@ done:
 	device->version_count = count;
 
 	/* if the device support the QMI call SYNC over the CTL interface */
-	if ((device->control_major == 1 && device->control_minor >= 5) ||
-			device->control_major > 1) {
+	if ((qmux->control_major == 1 && qmux->control_minor >= 5) ||
+			qmux->control_major > 1) {
 		qmi_device_sync(data->device, data);
 		return;
 	}
Signature · 1 line
2.43.0
#4 lore raw

PATCH 5/7 qmi: Move version_str to qmux subclass

In reply to Denis Kenzior

The version_str member is only available on QMUX based devices, and only on certain ones. Move this member out to the qmux subclass and add a debug statement to print it out if available.

---
 drivers/qmimodem/qmi.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/qmimodem/qmi.c +4 -3
index dae540251118..47a9bbe2cb59 100644
@@ -73,7 +73,6 @@ struct qmi_device {
 	uint16_t next_service_tid;
 	qmi_debug_func_t debug_func;
 	void *debug_data;
-	char *version_str;
 	struct qmi_version *version_list;
 	uint8_t version_count;
 	GHashTable *service_list;
@@ -91,6 +90,7 @@ struct qmi_device_qmux {
 	struct qmi_device super;
 	uint16_t control_major;
 	uint16_t control_minor;
+	char *version_str;
 };
 
 struct qmi_service {
@@ -1052,7 +1052,6 @@ void qmi_device_unref(struct qmi_device *device)
 
 	g_hash_table_destroy(device->service_list);
 
-	g_free(device->version_str);
 	g_free(device->version_list);
 
 	if (device->shutting_down)
@@ -1269,7 +1268,8 @@ static void discover_callback(uint16_t message, uint16_t length,
 	if (!ptr)
 		goto done;
 
-	device->version_str = strndup(ptr + 1, *((uint8_t *) ptr));
+	qmux->version_str = l_strndup(ptr + 1, *((uint8_t *) ptr));
+	__debug_device(device, "version string: %s", qmux->version_str);
 
 done:
 	device->version_list = list;
@@ -1648,6 +1648,7 @@ static void qmi_device_qmux_destroy(struct qmi_device *device)
 	struct qmi_device_qmux *qmux =
 		l_container_of(device, struct qmi_device_qmux, super);
 
+	l_free(qmux->version_str);
 	l_free(qmux);
 }
 
Signature · 1 line
2.43.0
#5 lore raw

PATCH 6/7 qmi: Introduce shutdown operation

In reply to Denis Kenzior

On QMUX the shutdown operation waits for all low-level service handles to be released, then calls the shutdown callback / destroy functions. Low level service handles on QMUX protocol are released via qmi_service_unref, which in turn sends a QMUX control message.

On the other hand, in QRTR, there is no need to explicitly release handles. They are released automatically when the underlying QRTR socket is closed.

Abstract the shutdown operation behind a shutdown method in the ops structure, and move the QMUX specific implementation there. While here, change the return signature of qmi_device_shutdown to return an int, such that better error reporting can be supported. For example, QRTR might choose not to implement the shutdown method entirely, in which case an -EOPNOTSUP will be returned.

---
 drivers/qmimodem/qmi.c | 129 ++++++++++++++++++++++++-----------------
 drivers/qmimodem/qmi.h |   2 +-
 2 files changed, 77 insertions(+), 54 deletions(-)
drivers/qmimodem/qmi.c +76 -53
index 47a9bbe2cb59..5543df027c15 100644
@@ -56,6 +56,9 @@ struct qmi_version {
 };
 
 struct qmi_device_ops {
+	int (*shutdown)(struct qmi_device *device,
+			qmi_shutdown_func_t shutdown_func,
+			void *user, qmi_destroy_func_t destroy);
 	void (*destroy)(struct qmi_device *device);
 };
 
@@ -77,10 +80,6 @@ struct qmi_device {
 	uint8_t version_count;
 	GHashTable *service_list;
 	unsigned int release_users;
-	qmi_shutdown_func_t shutdown_func;
-	void *shutdown_user_data;
-	qmi_destroy_func_t shutdown_destroy;
-	guint shutdown_source;
 	const struct qmi_device_ops *ops;
 	bool shutting_down : 1;
 	bool destroyed : 1;
@@ -91,6 +90,10 @@ struct qmi_device_qmux {
 	uint16_t control_major;
 	uint16_t control_minor;
 	char *version_str;
+	qmi_shutdown_func_t shutdown_func;
+	void *shutdown_user_data;
+	qmi_destroy_func_t shutdown_destroy;
+	guint shutdown_source;
 };
 
 struct qmi_service {
@@ -1017,6 +1020,12 @@ struct qmi_device *qmi_device_ref(struct qmi_device *device)
 	return device;
 }
 
+static void __qmi_device_shutdown_finished(struct qmi_device *device)
+{
+	if (device->destroyed)
+		device->ops->destroy(device);
+}
+
 void qmi_device_unref(struct qmi_device *device)
 {
 	if (!device)
@@ -1047,9 +1056,6 @@ void qmi_device_unref(struct qmi_device *device)
 
 	close(device->fd);
 
-	if (device->shutdown_source)
-		g_source_remove(device->shutdown_source);
-
 	g_hash_table_destroy(device->service_list);
 
 	g_free(device->version_list);
@@ -1393,58 +1399,16 @@ static void release_client(struct qmi_device *device,
 	__request_submit(device, req);
 }
 
-static void shutdown_destroy(gpointer user_data)
-{
-	struct qmi_device *device = user_data;
-
-	if (device->shutdown_destroy)
-		device->shutdown_destroy(device->shutdown_user_data);
-
-	device->shutdown_source = 0;
-
-	if (device->destroyed)
-		g_free(device);
-}
-
-static gboolean shutdown_callback(gpointer user_data)
-{
-	struct qmi_device *device = user_data;
-
-	if (device->release_users > 0)
-		return TRUE;
-
-	device->shutting_down = true;
-
-	if (device->shutdown_func)
-		device->shutdown_func(device->shutdown_user_data);
-
-	device->shutting_down = true;
-
-	return FALSE;
-}
-
-bool qmi_device_shutdown(struct qmi_device *device, qmi_shutdown_func_t func,
+int qmi_device_shutdown(struct qmi_device *device, qmi_shutdown_func_t func,
 				void *user_data, qmi_destroy_func_t destroy)
 {
 	if (!device)
 		return false;
 
-	if (device->shutdown_source > 0)
-		return false;
-
-	__debug_device(device, "device %p shutdown", device);
-
-	device->shutdown_source = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT,
-						0, shutdown_callback, device,
-						shutdown_destroy);
-	if (device->shutdown_source == 0)
-		return false;
+	if (!device->ops->shutdown)
+		return -ENOTSUP;
 
-	device->shutdown_func = func;
-	device->shutdown_user_data = user_data;
-	device->shutdown_destroy = destroy;
-
-	return true;
+	return device->ops->shutdown(device, func, user_data, destroy);
 }
 
 static bool get_device_file_name(struct qmi_device *device,
@@ -1643,16 +1607,75 @@ done:
 	return res;
 }
 
+static void qmux_shutdown_destroy(gpointer user_data)
+{
+	struct qmi_device_qmux *qmux = user_data;
+
+	if (qmux->shutdown_destroy)
+		qmux->shutdown_destroy(qmux->shutdown_user_data);
+
+	qmux->shutdown_source = 0;
+
+	__qmi_device_shutdown_finished(&qmux->super);
+}
+
+static gboolean qmux_shutdown_callback(gpointer user_data)
+{
+	struct qmi_device_qmux *qmux = user_data;
+
+	if (qmux->super.release_users > 0)
+		return TRUE;
+
+	qmux->super.shutting_down = true;
+
+	if (qmux->shutdown_func)
+		qmux->shutdown_func(qmux->shutdown_user_data);
+
+	qmux->super.shutting_down = false;
+
+	return FALSE;
+}
+
+static int qmi_device_qmux_shutdown(struct qmi_device *device,
+					qmi_shutdown_func_t func,
+					void *user_data,
+					qmi_destroy_func_t destroy)
+{
+	struct qmi_device_qmux *qmux =
+		l_container_of(device, struct qmi_device_qmux, super);
+
+	if (qmux->shutdown_source > 0)
+		return -EALREADY;
+
+	__debug_device(&qmux->super, "device %p shutdown", &qmux->super);
+
+	qmux->shutdown_source = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT,
+						0, qmux_shutdown_callback,
+						qmux, qmux_shutdown_destroy);
+	if (qmux->shutdown_source == 0)
+		return -EIO;
+
+	qmux->shutdown_func = func;
+	qmux->shutdown_user_data = user_data;
+	qmux->shutdown_destroy = destroy;
+
+	return 0;
+}
+
 static void qmi_device_qmux_destroy(struct qmi_device *device)
 {
 	struct qmi_device_qmux *qmux =
 		l_container_of(device, struct qmi_device_qmux, super);
 
+	if (qmux->shutdown_source)
+		g_source_remove(qmux->shutdown_source);
+
 	l_free(qmux->version_str);
 	l_free(qmux);
 }
 
 static const struct qmi_device_ops qmux_ops = {
+	.shutdown = qmi_device_qmux_shutdown,
 	.destroy = qmi_device_qmux_destroy,
 };
 
drivers/qmimodem/qmi.h +1 -1
index ab0ee479c89d..df4bc39644a2 100644
@@ -89,7 +89,7 @@ void qmi_device_set_debug(struct qmi_device *device,
 
 bool qmi_device_discover(struct qmi_device *device, qmi_discover_func_t func,
 				void *user_data, qmi_destroy_func_t destroy);
-bool qmi_device_shutdown(struct qmi_device *device, qmi_shutdown_func_t func,
+int qmi_device_shutdown(struct qmi_device *device, qmi_shutdown_func_t func,
 				void *user_data, qmi_destroy_func_t destroy);
 
 bool qmi_device_has_service(struct qmi_device *device, uint16_t type);
Signature · 1 line
2.43.0
#6 lore raw

PATCH 7/7 gobi: Fail if device could not be created

In reply to Denis Kenzior

If for some reason the qmux device creation fails, indicate failure early and do not rely on the 5 second modem powered timeout.

---
 plugins/gobi.c | 2 ++
 1 file changed, 2 insertions(+)
plugins/gobi.c +2 -0
index e22b8146cf93..4134c62d398e 100644
@@ -431,6 +431,8 @@ static int gobi_enable(struct ofono_modem *modem)
 		return -EINVAL;
 
 	data->device = qmi_device_new_qmux(device);
+	if (!data->device)
+		return -EIO;
 
 	if (getenv("OFONO_QMI_DEBUG"))
 		qmi_device_set_debug(data->device, gobi_debug, "QMI: ");
Signature · 1 line
2.43.0