From deccf35a68063ddae4a51a9ec99124fce998cf4b Mon Sep 17 00:00:00 2001
From: Dylan Van Assche <me@dylanvanassche.be>
Date: Sat, 30 Jan 2021 21:57:50 +0100
Subject: [PATCH] mm-broadband-modem-qmi: Add
 ID_MM_QMI_MESSAGING_AT_FALLBACK_ENABLE

Allows to force a fallback to AT URCs for detecting
messaging events instead of QMI indications.
This allows to receive text messages during suspend
on the PinePhone with a Quectel EG25 modem.
---
 .../quectel/77-mm-quectel-port-types.rules    |  2 ++
 src/mm-broadband-modem-qmi.c                  | 34 +++++++++++++++++--
 src/mm-broadband-modem-qmi.h                  |  1 +
 3 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/plugins/quectel/77-mm-quectel-port-types.rules b/plugins/quectel/77-mm-quectel-port-types.rules
index 54778d24..fab1f6fa 100644
--- a/plugins/quectel/77-mm-quectel-port-types.rules
+++ b/plugins/quectel/77-mm-quectel-port-types.rules
@@ -48,10 +48,12 @@ ATTRS{idVendor}=="2c7c", ATTRS{idProduct}=="0296", ENV{.MM_USBIFNUM}=="02", ENV{
 ATTRS{idVendor}=="2c7c", ATTRS{idProduct}=="0296", ENV{.MM_USBIFNUM}=="03", ENV{ID_MM_PORT_TYPE_AT_SECONDARY}="1"
 
 # Quectel EC25/EG25
+#  Always fallback to AT URCs for messaging notifications
 #  ttyUSB0 (if #0): QCDM/DIAG port
 #  ttyUSB1 (if #1): GPS data port
 #  ttyUSB2 (if #2): AT primary port
 #  ttyUSB3 (if #3): AT secondary port
+ATTRS{idVendor}=="2c7c", ATTRS{idProduct}=="0125", ENV{ID_MM_QMI_MESSAGING_AT_FALLBACK_ENABLE}="1"
 ATTRS{idVendor}=="2c7c", ATTRS{idProduct}=="0125", ENV{.MM_USBIFNUM}=="00", ENV{ID_MM_PORT_TYPE_QCDM}="1"
 ATTRS{idVendor}=="2c7c", ATTRS{idProduct}=="0125", ENV{.MM_USBIFNUM}=="01", ENV{ID_MM_PORT_TYPE_GPS}="1"
 ATTRS{idVendor}=="2c7c", ATTRS{idProduct}=="0125", ENV{.MM_USBIFNUM}=="02", ENV{ID_MM_PORT_TYPE_AT_PRIMARY}="1"
diff --git a/src/mm-broadband-modem-qmi.c b/src/mm-broadband-modem-qmi.c
index ae27b333..4735e616 100644
--- a/src/mm-broadband-modem-qmi.c
+++ b/src/mm-broadband-modem-qmi.c
@@ -173,6 +173,30 @@ shared_qmi_peek_client (MMSharedQmi    *self,
     return client;
 }
 
+/*****************************************************************************/
+
+MMPortQmi *
+mm_broadband_modem_qmi_peek_port_qmi (MMBroadbandModemQmi *self)
+{
+    MMPortQmi *primary_qmi_port = NULL;
+    GList     *qmi_ports;
+
+    g_assert (MM_IS_BROADBAND_MODEM_QMI (self));
+
+    qmi_ports = mm_base_modem_find_ports (MM_BASE_MODEM (self),
+                                          MM_PORT_SUBSYS_UNKNOWN,
+                                          MM_PORT_TYPE_QMI,
+                                          NULL);
+
+    /* First QMI port in the list is the primary one always */
+    if (qmi_ports)
+        primary_qmi_port = MM_PORT_QMI (qmi_ports->data);
+
+    g_list_free_full (qmi_ports, g_object_unref);
+
+    return primary_qmi_port;
+}
+
 /*****************************************************************************/
 /* Create Bearer (Modem interface) */
 
@@ -6186,9 +6210,12 @@ messaging_cleanup_unsolicited_events (MMIfaceModemMessaging *_self,
                                       gpointer user_data)
 {
     MMBroadbandModemQmi *self = MM_BROADBAND_MODEM_QMI (_self);
+    MMPort *port = MM_PORT (mm_broadband_modem_qmi_peek_port_qmi (self));
 
     /* Handle fallback */
-    if (self->priv->messaging_fallback_at) {
+    if (self->priv->messaging_fallback_at ||
+        mm_kernel_device_get_global_property_as_boolean (mm_port_peek_kernel_device (port),
+                                                         "ID_MM_QMI_MESSAGING_AT_FALLBACK_ENABLE")) {
         return iface_modem_messaging_parent->cleanup_unsolicited_events (_self, callback, user_data);
     }
 
@@ -6204,9 +6231,12 @@ messaging_setup_unsolicited_events (MMIfaceModemMessaging *_self,
                                     gpointer user_data)
 {
     MMBroadbandModemQmi *self = MM_BROADBAND_MODEM_QMI (_self);
+    MMPort *port = MM_PORT (mm_broadband_modem_qmi_peek_port_qmi (self));
 
     /* Handle fallback */
-    if (self->priv->messaging_fallback_at) {
+    if (self->priv->messaging_fallback_at ||
+        mm_kernel_device_get_global_property_as_boolean (mm_port_peek_kernel_device (port),
+                                                         "ID_MM_QMI_MESSAGING_AT_FALLBACK_ENABLE")) {
         return iface_modem_messaging_parent->setup_unsolicited_events (_self, callback, user_data);
     }
 
diff --git a/src/mm-broadband-modem-qmi.h b/src/mm-broadband-modem-qmi.h
index 23825068..c9b50f15 100644
--- a/src/mm-broadband-modem-qmi.h
+++ b/src/mm-broadband-modem-qmi.h
@@ -46,5 +46,6 @@ MMBroadbandModemQmi *mm_broadband_modem_qmi_new (const gchar *device,
                                                  const gchar *plugin,
                                                  guint16 vendor_id,
                                                  guint16 product_id);
+MMPortQmi *mm_broadband_modem_qmi_peek_port_qmi  (MMBroadbandModemQmi  *self);
 
 #endif /* MM_BROADBAND_MODEM_QMI_H */
-- 
2.30.0
