Hi Martin,

On 7/10/19 4:51 PM, Martin Hundebøll wrote:
Setup GSM 07.10 multiplexing using the kernel n_gsm line discpline
driver, and use the virtual tty devices as Aux and Modem channels.

The driver supports rts/cts on the underlying serial device. This is
enabled with OFONO_QUECTED_RTSCTS udev environment, e.g.:

KERNEL=="ttymxc0", ENV{OFONO_DRIVER}="quectel", \
         ENV{OFONO_QUECTEL_RTSCTS}="on"
---

Changes since v1:
  * work around kernel dead-lock in n_gsm tty line discipline
  * clearify explanatory comment in cfun_query()
  * add error handling to cfun_query()
  * move hard-coding of gsm tty paths from udevng to quectel

  plugins/quectel.c | 275 ++++++++++++++++++++++++++++++++++++++++------
  plugins/udevng.c  |  24 +++-
  2 files changed, 267 insertions(+), 32 deletions(-)

diff --git a/plugins/quectel.c b/plugins/quectel.c
index 6e93f45a..8914cf45 100644
--- a/plugins/quectel.c
+++ b/plugins/quectel.c
@@ -25,7 +25,13 @@
#include <errno.h>
  #include <stdlib.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <linux/tty.h>
+#include <linux/gsmmux.h>
  #include <ell/ell.h>
  #include <gatchat.h>
  #include <gattty.h>
@@ -47,11 +53,28 @@ static const char *cfun_prefix[] = { "+CFUN:", NULL };
  static const char *cpin_prefix[] = { "+CPIN:", NULL };
  static const char *none_prefix[] = { NULL };
+static const uint8_t gsm0710_terminate[] = {
+       0xf9, /* open flag */
+       0x03, /* channel 0 */
+       0xef, /* UIH frame */
+       0x05, /* 2 data bytes */
+       0xc3, /* terminate 1 */
+       0x01, /* terminate 2 */
+       0xf2, /* crc */
+       0xf9, /* close flag */
+};
+

I still say all these linux gsm mux details belong in a utility file...

  struct quectel_data {
        GAtChat *modem;
        GAtChat *aux;
        guint cpin_ready;
-       gboolean have_sim;
+       bool have_sim;
+
+       /* used by quectel uart driver */
+       GAtChat *uart;
+       struct l_timeout *mux_ready_timer;

Removed this unused variable

+       int mux_ready_count;
+       int initial_ldisc;
  };
static void quectel_debug(const char *str, void *user_data)
@@ -86,9 +109,67 @@ static void quectel_remove(struct ofono_modem *modem)
        ofono_modem_set_data(modem, NULL);
        g_at_chat_unref(data->aux);
        g_at_chat_unref(data->modem);
+       g_at_chat_unref(data->uart);
        l_free(data);
  }
+static void close_mux_cb(struct l_timeout *timeout, void *user_data)
+{
+       struct ofono_modem *modem = user_data;
+       struct quectel_data *data = ofono_modem_get_data(modem);
+       GIOChannel *device;
+       uint32_t gpio_value = 0;
+       ssize_t write_count;
+       int fd;
+
+       DBG("%p", modem);
+
+       device = g_at_chat_get_channel(data->uart);
+       fd = g_io_channel_unix_get_fd(device);
+
+       /* restore initial tty line discipline */
+       if (ioctl(fd, TIOCSETD, &data->initial_ldisc) < 0)
+               ofono_warn("Failed to restore line discipline");
+
+       /* terminate gsm 0710 multiplexing on the modem side */
+       write_count = write(fd, gsm0710_terminate, sizeof(gsm0710_terminate));
+       if (write_count != sizeof(gsm0710_terminate))
+               ofono_warn("Failed to terminate gsm multiplexing");
+
+       g_at_chat_unref(data->uart);
+       data->uart = NULL;
+
+       l_timeout_remove(timeout);
+       ofono_modem_set_powered(modem, false);

Changed 'false' to 'FALSE' here since it is an ofono_bool_t

+}
+
+static void close_serial(struct ofono_modem *modem)
+{
+       struct quectel_data *data = ofono_modem_get_data(modem);
+
+       DBG("%p", modem);
+
+       g_at_chat_unref(data->aux);
+       data->aux = NULL;
+
+       g_at_chat_unref(data->modem);
+       data->modem = NULL;
+
+       /*
+        * if gsm0710 multiplexing is used, the aux and modem file descriptors
+        * must be closed before closing the underlying serial device to avoid
+        * an old kernel dead-lock:
+        * https://lists.ofono.org/pipermail/ofono/2011-March/009405.html
+        *
+        * setup a timer to iterate the mainloop once to let gatchat close the
+        * virtual file descriptors unreferenced above
+        */
+       if (data->uart)
+               l_timeout_create_ms(1, close_mux_cb, modem, NULL);

I wonder if you should be paranoid and track this timer in case close_mux_cb is never called. E.g. if the device is forcefully removed (perhaps a usb<->serial converter is used)?

+       else
+               ofono_modem_set_powered(modem, false);
+}
+
  static void cpin_notify(GAtResult *result, gpointer user_data)
  {
        struct ofono_modem *modem = user_data;
@@ -106,7 +187,7 @@ static void cpin_notify(GAtResult *result, gpointer 
user_data)
        g_at_result_iter_next_unquoted_string(&iter, &sim_inserted);
if (g_strcmp0(sim_inserted, "NOT INSERTED") != 0)
-               data->have_sim = TRUE;
+               data->have_sim = true;
ofono_modem_set_powered(modem, TRUE); @@ -133,11 +214,7 @@ static void cfun_enable(gboolean ok, GAtResult *result, gpointer user_data)
        DBG("%p ok %d", modem, ok);
if (!ok) {
-               g_at_chat_unref(data->aux);
-               data->aux = NULL;
-               g_at_chat_unref(data->modem);
-               data->modem = NULL;
-               ofono_modem_set_powered(modem, FALSE);
+               close_serial(modem);
                return;
        }
@@ -152,19 +229,23 @@ static void cfun_query(gboolean ok, GAtResult *result, gpointer user_data)
        struct ofono_modem *modem = user_data;
        struct quectel_data *data = ofono_modem_get_data(modem);
        GAtResultIter iter;
-       int status;
+       int cfun;
DBG("%p ok %d", modem, ok); - if (!ok)
+       if (!ok) {
+               close_serial(modem);
                return;
+       }
g_at_result_iter_init(&iter, result); - if (g_at_result_iter_next(&iter, "+CFUN:") == FALSE)
+       if (g_at_result_iter_next(&iter, "+CFUN:") == FALSE) {
+               close_serial(modem);
                return;
+       }
- g_at_result_iter_next_number(&iter, &status);
+       g_at_result_iter_next_number(&iter, &cfun);
/*
         * The modem firmware powers up in CFUN=1 but will respond to AT+CFUN=4
@@ -172,20 +253,18 @@ static void cfun_query(gboolean ok, GAtResult *result, 
gpointer user_data)
         * passes.  Empirical evidence suggests that the firmware will report an
         * unsolicited +CPIN: notification when it is ready to be useful.
         *
-        * Work around this feature by only transitioning to CFUN=4 after we've
-        * received an unsolicited +CPIN: notification.
+        * Work around this feature by only transitioning to CFUN=4 if the
+        * modem is not in CFUN=1 or untill after we've received an unsolicited

Fixed typo: 'untill' -> 'until'

+        * +CPIN: notification.
         */
-
-       if (status != 1) {
+       if (cfun != 1)
                g_at_chat_send(data->aux, "AT+CFUN=4", none_prefix, cfun_enable,
                                modem, NULL);
-               return;
-       }
-
-       cfun_enable(TRUE, NULL, modem);
+       else
+               cfun_enable(TRUE, NULL, modem);
  }
-static int quectel_enable(struct ofono_modem *modem)
+static int open_ttys(struct ofono_modem *modem)
  {
        struct quectel_data *data = ofono_modem_get_data(modem);
@@ -216,18 +295,155 @@ static int quectel_enable(struct ofono_modem *modem)
        return -EINPROGRESS;
  }
-static void cfun_disable(gboolean ok, GAtResult *result, gpointer user_data)
+static void mux_ready_cb(struct l_timeout *timeout, void *user_data)
  {
        struct ofono_modem *modem = user_data;
        struct quectel_data *data = ofono_modem_get_data(modem);
+       struct stat st;
+       int i, ret;
DBG("%p", modem); - g_at_chat_unref(data->aux);
-       data->aux = NULL;
+       /* check if the last (and thus all) virtual gsm tty's are created */
+       ret = stat(ofono_modem_get_string(modem, "Modem"), &st);
+       if (ret < 0) {
+               if (data->mux_ready_count++ < 5) {
+                       /* not ready yet; try again in 100 ms*/
+                       l_timeout_modify_ms(timeout, 100);
+                       return;
+               }
+
+               /* not ready after 500 ms; bail out */
+               close_serial(modem);
+               return;
+       }
- if (ok)
-               ofono_modem_set_powered(modem, FALSE);
+       /* virtual gsm tty's are ready */
+       l_timeout_remove(timeout);
+
+       if (open_ttys(modem) != -EINPROGRESS)
+               close_serial(modem);
+
+       g_at_chat_set_slave(data->uart, data->modem);
+}
+
+static void cmux_cb(gboolean ok, GAtResult *result, gpointer user_data)
+{
+       struct ofono_modem *modem = user_data;
+       struct quectel_data *data = ofono_modem_get_data(modem);
+       struct gsm_config gsm_config;
+       GIOChannel *device;
+       int ldisc = N_GSM0710;
+       int fd;
+
+       DBG("%p", modem);
+
+       device = g_at_chat_get_channel(data->uart);
+       fd = g_io_channel_unix_get_fd(device);
+
+       /* get initial line discipline to restore after use */
+       if (ioctl(fd, TIOCGETD, &data->initial_ldisc) < 0) {
+               ofono_error("Failed to get current line discipline: %s", 
strerror(errno));

Fixed over 80 chars / line..

+               close_serial(modem);
+               return;
+       }
+
+       /* enable gsm 0710 multiplexing line discipline */
+       if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
+               ofono_error("Failed to set multiplexer line discipline: %s", 
strerror(errno));

And here

+               close_serial(modem);
+               return;
+       }
+
+       /* get n_gsm configuration */
+       if (ioctl(fd, GSMIOC_GETCONF, &gsm_config) < 0) {
+               ofono_error("Failed to get gsm config: %s", strerror(errno));
+               close_serial(modem);
+               return;
+       }
+
+       gsm_config.initiator = 1;     /* cpu side is initiating multiplexing */
+       gsm_config.encapsulation = 0; /* basic transparency encoding */
+       gsm_config.mru = 127;         /* 127 bytes rx mtu */
+       gsm_config.mtu = 127;         /* 127 bytes tx mtu */
+       gsm_config.t1 = 10;           /* 100 ms ack timer */
+       gsm_config.n2 = 3;            /* 3 retries */
+       gsm_config.t2 = 30;           /* 300 ms response timer */
+       gsm_config.t3 = 10;           /* 100 ms wake up response timer */
+       gsm_config.i = 1;             /* subset */
+
+       /* set the new configuration */
+       if (ioctl(fd, GSMIOC_SETCONF, &gsm_config) < 0) {
+               ofono_error("Failed to set gsm config: %s", strerror(errno));
+               close_serial(modem);
+               return;
+       }
+
+       /*
+        * the kernel does not yet support mapping the underlying serial device
+        * to its virtual gsm ttys, so hard-code gsmtty1 gsmtty2 for now
+        */
+       ofono_modem_set_string(modem, "Aux", "/dev/gsmtty1");
+       ofono_modem_set_string(modem, "Modem", "/dev/gsmtty2");
+
+       /* wait for gsmtty devices to appear */
+       if (!l_timeout_create_ms(100, mux_ready_cb, modem, NULL)) {

Same comment here. It seems the unused l_timeout variable was intended to be used here...

+               close_serial(modem);
+               return;
+       }
+}
+
+static int open_serial(struct ofono_modem *modem)
+{
+       struct quectel_data *data = ofono_modem_get_data(modem);
+       const char *rts_cts;
+
+       DBG("%p", modem);
+
+       rts_cts = ofono_modem_get_string(modem, "RtsCts");
+
+       data->uart = at_util_open_device(modem, "Device", quectel_debug,
+                                               "UART: ",
+                                               "Baud", "115200",
+                                               "Parity", "none",
+                                               "StopBits", "1",
+                                               "DataBits", "8",
+                                               "XonXoff", "off",
+                                               "Local", "on",
+                                               "Read", "on",
+                                               "RtsCts", rts_cts,
+                                               NULL);
+       if (data->uart == NULL)
+               return -EINVAL;
+
+       g_at_chat_send(data->uart, "ATE0", none_prefix, NULL, NULL,
+                       NULL);
+
+       /* setup multiplexing */
+       g_at_chat_send(data->uart, "AT+CMUX=0,0,5,127,10,3,30,10,2", NULL,
+                       cmux_cb, modem, NULL);
+
+       return -EINPROGRESS;
+}
+
+static int quectel_enable(struct ofono_modem *modem)
+{
+       DBG("%p", modem);
+
+       if (ofono_modem_get_string(modem, "Device"))
+               return open_serial(modem);
+       else
+               return open_ttys(modem);
+}
+
+static void cfun_disable(gboolean ok, GAtResult *result, gpointer user_data)
+{
+       struct ofono_modem *modem = user_data;
+       struct quectel_data *data = ofono_modem_get_data(modem);
+
+       DBG("%p", modem);
+
+       close_serial(modem);
  }
static int quectel_disable(struct ofono_modem *modem)
@@ -239,14 +455,11 @@ static int quectel_disable(struct ofono_modem *modem)
        g_at_chat_cancel_all(data->modem);
        g_at_chat_unregister_all(data->modem);
- g_at_chat_unref(data->modem);
-       data->modem = NULL;
-
        g_at_chat_cancel_all(data->aux);
        g_at_chat_unregister_all(data->aux);
- g_at_chat_send(data->aux, "AT+CFUN=0", cfun_prefix,
-                                       cfun_disable, modem, NULL);
+       g_at_chat_send(data->aux, "AT+CFUN=0", cfun_prefix, cfun_disable, modem,
+                       NULL);
return -EINPROGRESS;
  }
@@ -292,7 +505,7 @@ static void quectel_pre_sim(struct ofono_modem *modem)
        sim = ofono_sim_create(modem, OFONO_VENDOR_QUECTEL, "atmodem",
                                data->aux);
- if (sim && data->have_sim == TRUE)
+       if (sim && data->have_sim == true)
                ofono_sim_inserted_notify(sim, TRUE);
  }
diff --git a/plugins/udevng.c b/plugins/udevng.c
index 4b420dc0..567196bc 100644
--- a/plugins/udevng.c
+++ b/plugins/udevng.c
@@ -837,7 +837,7 @@ static gboolean setup_samsung(struct modem_info *modem)
        return TRUE;
  }
-static gboolean setup_quectel(struct modem_info *modem)
+static gboolean setup_quectel_usb(struct modem_info *modem)
  {
        const char *aux = NULL, *mdm = NULL;
        GSList *list;
@@ -877,6 +877,28 @@ static gboolean setup_quectel(struct modem_info *modem)
        return TRUE;
  }
+static gboolean setup_quectel_serial(struct modem_info *modem)
+{
+       struct serial_device_info* info;
+       const char *value;
+
+       info = modem->serial;

Changed this to initialize at declaration instead

+       value = udev_device_get_property_value(info->dev, 
"OFONO_QUECTEL_RTSCTS");
+

Fixed over 80 chars / line.

+       ofono_modem_set_string(modem->modem, "RtsCts", value ? value : "off");
+       ofono_modem_set_string(modem->modem, "Device", info->devnode);
+
+       return TRUE;
+}
+
+static gboolean setup_quectel(struct modem_info *modem)
+{
+       if (modem->serial)
+               return setup_quectel_serial(modem);
+       else
+               return setup_quectel_usb(modem);
+}
+
  static gboolean setup_quectelqmi(struct modem_info *modem)
  {
        const char *qmi = NULL, *net = NULL, *gps = NULL, *aux = NULL;


Applied, thanks.

Regards,
-Denis
_______________________________________________
ofono mailing list
ofono@ofono.org
https://lists.ofono.org/mailman/listinfo/ofono

Reply via email to