]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
net: wwan: add NMEA port support
authorSergey Ryazanov <ryazanov.s.a@gmail.com>
Mon, 26 Jan 2026 06:21:55 +0000 (14:21 +0800)
committerJakub Kicinski <kuba@kernel.org>
Sat, 31 Jan 2026 02:26:59 +0000 (18:26 -0800)
Many WWAN modems come with embedded GNSS receiver inside and have a
dedicated port to output geopositioning data. On the one hand, the
GNSS receiver has little in common with WWAN modem and just shares a
host interface and should be exported using the GNSS subsystem. On the
other hand, GNSS receiver is not automatically activated and needs a
generic WWAN control port (AT, MBIM, etc.) to be turned on. And a user
space software needs extra information to find the control port.

Introduce the new type of WWAN port - NMEA. When driver asks to register
a NMEA port, the core allocates common parent WWAN device as usual, but
exports the NMEA port via the GNSS subsystem and acts as a proxy between
the device driver and the GNSS subsystem.

From the WWAN device driver perspective, a NMEA port is registered as a
regular WWAN port without any difference. And the driver interacts only
with the WWAN core. From the user space perspective, the NMEA port is a
GNSS device which parent can be used to enumerate and select the proper
control port for the GNSS receiver management.

CC: Slark Xiao <slark_xiao@163.com>
CC: Muhammad Nuzaihan <zaihan@unrealasia.net>
CC: Qiang Yu <quic_qianyu@quicinc.com>
CC: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
CC: Johan Hovold <johan@kernel.org>
Suggested-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
Signed-off-by: Sergey Ryazanov <ryazanov.s.a@gmail.com>
Reviewed-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
Link: https://patch.msgid.link/20260126062158.308598-6-slark_xiao@163.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/wwan/Kconfig
drivers/net/wwan/wwan_core.c
include/linux/wwan.h

index 410b0245114e892808f1a445e34f954376e5d2ae..88df55d78d902f4f0fcb71282466d41f1ef78bdb 100644 (file)
@@ -7,6 +7,7 @@ menu "Wireless WAN"
 
 config WWAN
        tristate "WWAN Driver Core"
+       depends on GNSS || GNSS = n
        help
          Say Y here if you want to use the WWAN driver core. This driver
          provides a common framework for WWAN drivers.
index 1d9f43ef9b715913f64812a98b53f67ba3dd331d..015213b3d687a986edf28586dd40f1c4013284fc 100644 (file)
@@ -1,5 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0-only
-/* Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org> */
+/* WWAN Driver Core
+ *
+ * Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org>
+ * Copyright (c) 2025, Sergey Ryazanov <ryazanov.s.a@gmail.com>
+ */
 
 #include <linux/bitmap.h>
 #include <linux/err.h>
@@ -16,6 +20,7 @@
 #include <linux/types.h>
 #include <linux/uaccess.h>
 #include <linux/termios.h>
+#include <linux/gnss.h>
 #include <linux/wwan.h>
 #include <net/rtnetlink.h>
 #include <uapi/linux/wwan.h>
@@ -75,6 +80,7 @@ struct wwan_device {
  * @headroom_len: SKB reserved headroom size
  * @frag_len: Length to fragment packet
  * @at_data: AT port specific data
+ * @gnss: Pointer to GNSS device associated with this port
  */
 struct wwan_port {
        enum wwan_port_type type;
@@ -93,9 +99,16 @@ struct wwan_port {
                        struct ktermios termios;
                        int mdmbits;
                } at_data;
+               struct gnss_device *gnss;
        };
 };
 
+static int wwan_port_op_start(struct wwan_port *port);
+static void wwan_port_op_stop(struct wwan_port *port);
+static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb,
+                          bool nonblock);
+static int wwan_wait_tx(struct wwan_port *port, bool nonblock);
+
 static ssize_t index_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
        struct wwan_device *wwan = to_wwan_dev(dev);
@@ -336,6 +349,7 @@ static const struct {
                .name = "MIPC",
                .devsuf = "mipc",
        },
+       /* WWAN_PORT_NMEA is exported via the GNSS subsystem */
 };
 
 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
@@ -487,6 +501,124 @@ static void wwan_port_unregister_wwan(struct wwan_port *port)
        device_del(&port->dev);
 }
 
+#if IS_ENABLED(CONFIG_GNSS)
+static int wwan_gnss_open(struct gnss_device *gdev)
+{
+       return wwan_port_op_start(gnss_get_drvdata(gdev));
+}
+
+static void wwan_gnss_close(struct gnss_device *gdev)
+{
+       wwan_port_op_stop(gnss_get_drvdata(gdev));
+}
+
+static int wwan_gnss_write(struct gnss_device *gdev, const unsigned char *buf,
+                          size_t count)
+{
+       struct wwan_port *port = gnss_get_drvdata(gdev);
+       struct sk_buff *skb, *head = NULL, *tail = NULL;
+       size_t frag_len, remain = count;
+       int ret;
+
+       ret = wwan_wait_tx(port, false);
+       if (ret)
+               return ret;
+
+       do {
+               frag_len = min(remain, port->frag_len);
+               skb = alloc_skb(frag_len + port->headroom_len, GFP_KERNEL);
+               if (!skb) {
+                       ret = -ENOMEM;
+                       goto freeskb;
+               }
+               skb_reserve(skb, port->headroom_len);
+               memcpy(skb_put(skb, frag_len), buf + count - remain, frag_len);
+
+               if (!head) {
+                       head = skb;
+               } else {
+                       if (!tail)
+                               skb_shinfo(head)->frag_list = skb;
+                       else
+                               tail->next = skb;
+
+                       tail = skb;
+                       head->data_len += skb->len;
+                       head->len += skb->len;
+                       head->truesize += skb->truesize;
+               }
+       } while (remain -= frag_len);
+
+       ret = wwan_port_op_tx(port, head, false);
+       if (!ret)
+               return count;
+
+freeskb:
+       kfree_skb(head);
+       return ret;
+}
+
+static struct gnss_operations wwan_gnss_ops = {
+       .open = wwan_gnss_open,
+       .close = wwan_gnss_close,
+       .write_raw = wwan_gnss_write,
+};
+
+/* GNSS port specific device registration */
+static int wwan_port_register_gnss(struct wwan_port *port)
+{
+       struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
+       struct gnss_device *gdev;
+       int err;
+
+       gdev = gnss_allocate_device(&wwandev->dev);
+       if (!gdev)
+               return -ENOMEM;
+
+       /* NB: for now we support only NMEA WWAN port type, so hardcode
+        * the GNSS port type. If more GNSS WWAN port types will be added,
+        * then we should dynamically map WWAN port type to GNSS type.
+        */
+       gdev->type = GNSS_TYPE_NMEA;
+       gdev->ops = &wwan_gnss_ops;
+       gnss_set_drvdata(gdev, port);
+
+       port->gnss = gdev;
+
+       err = gnss_register_device(gdev);
+       if (err) {
+               gnss_put_device(gdev);
+               return err;
+       }
+
+       dev_info(&wwandev->dev, "port %s attached\n", dev_name(&gdev->dev));
+
+       return 0;
+}
+
+/* GNSS port specific device unregistration */
+static void wwan_port_unregister_gnss(struct wwan_port *port)
+{
+       struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
+       struct gnss_device *gdev = port->gnss;
+
+       dev_info(&wwandev->dev, "port %s disconnected\n", dev_name(&gdev->dev));
+
+       gnss_deregister_device(gdev);
+       gnss_put_device(gdev);
+}
+#else
+static int wwan_port_register_gnss(struct wwan_port *port)
+{
+       return -EOPNOTSUPP;
+}
+
+static void wwan_port_unregister_gnss(struct wwan_port *port)
+{
+       WARN_ON(1);     /* This handler cannot be called */
+}
+#endif
+
 struct wwan_port *wwan_create_port(struct device *parent,
                                   enum wwan_port_type type,
                                   const struct wwan_port_ops *ops,
@@ -527,7 +659,11 @@ struct wwan_port *wwan_create_port(struct device *parent,
        dev_set_drvdata(&port->dev, drvdata);
        device_initialize(&port->dev);
 
-       err = wwan_port_register_wwan(port);
+       if (port->type == WWAN_PORT_NMEA)
+               err = wwan_port_register_gnss(port);
+       else
+               err = wwan_port_register_wwan(port);
+
        if (err)
                goto error_put_device;
 
@@ -557,7 +693,10 @@ void wwan_remove_port(struct wwan_port *port)
        wake_up_interruptible(&port->waitqueue);
        skb_queue_purge(&port->rxq);
 
-       wwan_port_unregister_wwan(port);
+       if (port->type == WWAN_PORT_NMEA)
+               wwan_port_unregister_gnss(port);
+       else
+               wwan_port_unregister_wwan(port);
 
        put_device(&port->dev);
 
@@ -568,8 +707,15 @@ EXPORT_SYMBOL_GPL(wwan_remove_port);
 
 void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb)
 {
-       skb_queue_tail(&port->rxq, skb);
-       wake_up_interruptible(&port->waitqueue);
+       if (port->type == WWAN_PORT_NMEA) {
+#if IS_ENABLED(CONFIG_GNSS)
+               gnss_insert_raw(port->gnss, skb->data, skb->len);
+#endif
+               consume_skb(skb);
+       } else {
+               skb_queue_tail(&port->rxq, skb);
+               wake_up_interruptible(&port->waitqueue);
+       }
 }
 EXPORT_SYMBOL_GPL(wwan_port_rx);
 
index a4d6cc0c9f68bf363831766c4f996371faf6f578..1e0e2cb5357990fddfc482b4b3ab47fadc4463b7 100644 (file)
@@ -19,6 +19,7 @@
  * @WWAN_PORT_FASTBOOT: Fastboot protocol control
  * @WWAN_PORT_ADB: ADB protocol control
  * @WWAN_PORT_MIPC: MTK MIPC diagnostic interface
+ * @WWAN_PORT_NMEA: embedded GNSS receiver with NMEA output
  *
  * @WWAN_PORT_MAX: Highest supported port types
  * @WWAN_PORT_UNKNOWN: Special value to indicate an unknown port type
@@ -34,6 +35,7 @@ enum wwan_port_type {
        WWAN_PORT_FASTBOOT,
        WWAN_PORT_ADB,
        WWAN_PORT_MIPC,
+       WWAN_PORT_NMEA,
 
        /* Add new port types above this line */