This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 877a9af2d14 rpmsg_virtio/rptun: support fallback cpuname when 
VIRTIO_RPMSG_F_CPUNAME not set
877a9af2d14 is described below

commit 877a9af2d14cf6053deabe0df729a1b1efa6e1b6
Author: Bowen Wang <[email protected]>
AuthorDate: Wed Feb 4 20:36:59 2026 +0800

    rpmsg_virtio/rptun: support fallback cpuname when VIRTIO_RPMSG_F_CPUNAME 
not set
    
    When the VIRTIO_RPMSG_F_CPUNAME feature is not negotiated, the driver
    previously would crash due to DEBUGASSERT. This commit adds support
    for a fallback mechanism:
    
    1. Add rpmsg_virtio_probe_cpuname() API that accepts an optional cpuname
       parameter to be used when VIRTIO_RPMSG_F_CPUNAME is not available.
    
    2. Modify rptun driver to pass the cpuname from rptun device config
       to rpmsg_virtio_probe_cpuname().
    
    3. Refactor rptun device register/unregister to prioritize RPMSG device
       handling before other virtio device types.
    
    This ensures the system works correctly in scenarios where the remote
    side does not support the VIRTIO_RPMSG_F_CPUNAME feature.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/rpmsg/rpmsg_virtio.c       | 57 +++++++++++++++++++++++++-------------
 drivers/rptun/rptun.c              | 32 ++++++++++++---------
 include/nuttx/rpmsg/rpmsg_virtio.h |  2 ++
 3 files changed, 58 insertions(+), 33 deletions(-)

diff --git a/drivers/rpmsg/rpmsg_virtio.c b/drivers/rpmsg/rpmsg_virtio.c
index e28bb6b60d7..7f1f5cc9bc3 100644
--- a/drivers/rpmsg/rpmsg_virtio.c
+++ b/drivers/rpmsg/rpmsg_virtio.c
@@ -603,10 +603,11 @@ static int rpmsg_virtio_thread(int argc, FAR char *argv[])
  ****************************************************************************/
 
 /****************************************************************************
- * Name: rpmsg_virtio_probe
+ * Name: rpmsg_virtio_probe_cpuname
  ****************************************************************************/
 
-int rpmsg_virtio_probe(FAR struct virtio_device *vdev)
+int rpmsg_virtio_probe_cpuname(FAR struct virtio_device *vdev,
+                               FAR const char *cpuname)
 {
   FAR struct rpmsg_virtio_priv_s *priv;
   FAR char *argv[3];
@@ -639,28 +640,35 @@ int rpmsg_virtio_probe(FAR struct virtio_device *vdev)
 
   /* Read the virtio rpmsg config to get the local/remote cpu name  */
 
-  DEBUGASSERT(virtio_has_feature(vdev, VIRTIO_RPMSG_F_CPUNAME));
-  if (vdev->role == VIRTIO_DEV_DRIVER)
+  if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_CPUNAME))
     {
-      virtio_read_config_bytes(vdev, offsetof(struct fw_rsc_config,
-                                              host_cpuname),
-                               priv->rpmsg.local_cpuname,
-                               VIRTIO_RPMSG_CPUNAME_SIZE);
-      virtio_read_config_bytes(vdev, offsetof(struct fw_rsc_config,
-                                              remote_cpuname),
-                               priv->rpmsg.cpuname,
-                               VIRTIO_RPMSG_CPUNAME_SIZE);
+      if (vdev->role == VIRTIO_DEV_DRIVER)
+        {
+          virtio_read_config_bytes(vdev, offsetof(struct fw_rsc_config,
+                                                  host_cpuname),
+                                  priv->rpmsg.local_cpuname,
+                                  VIRTIO_RPMSG_CPUNAME_SIZE);
+          virtio_read_config_bytes(vdev, offsetof(struct fw_rsc_config,
+                                                  remote_cpuname),
+                                  priv->rpmsg.cpuname,
+                                  VIRTIO_RPMSG_CPUNAME_SIZE);
+        }
+      else
+        {
+          virtio_read_config_bytes(vdev, offsetof(struct fw_rsc_config,
+                                                  host_cpuname),
+                                  priv->rpmsg.cpuname,
+                                  VIRTIO_RPMSG_CPUNAME_SIZE);
+          virtio_read_config_bytes(vdev, offsetof(struct fw_rsc_config,
+                                                  remote_cpuname),
+                                  priv->rpmsg.local_cpuname,
+                                  VIRTIO_RPMSG_CPUNAME_SIZE);
+        }
     }
   else
     {
-      virtio_read_config_bytes(vdev, offsetof(struct fw_rsc_config,
-                                              host_cpuname),
-                               priv->rpmsg.cpuname,
-                               VIRTIO_RPMSG_CPUNAME_SIZE);
-      virtio_read_config_bytes(vdev, offsetof(struct fw_rsc_config,
-                                              remote_cpuname),
-                               priv->rpmsg.local_cpuname,
-                               VIRTIO_RPMSG_CPUNAME_SIZE);
+      DEBUGASSERT(cpuname != NULL);
+      strlcpy(priv->rpmsg.cpuname, cpuname, VIRTIO_RPMSG_CPUNAME_SIZE);
     }
 
   /* Register the rpmsg to rpmsg framework */
@@ -705,6 +713,15 @@ err:
   return ret;
 }
 
+/****************************************************************************
+ * Name: rpmsg_virtio_probe
+ ****************************************************************************/
+
+int rpmsg_virtio_probe(FAR struct virtio_device *vdev)
+{
+  return rpmsg_virtio_probe_cpuname(vdev, NULL);
+}
+
 /****************************************************************************
  * Name: rpmsg_virtio_remove
  ****************************************************************************/
diff --git a/drivers/rptun/rptun.c b/drivers/rptun/rptun.c
index 30a0cb37790..31a2d373541 100644
--- a/drivers/rptun/rptun.c
+++ b/drivers/rptun/rptun.c
@@ -544,10 +544,20 @@ static void rptun_remove_device(FAR struct rptun_priv_s 
*priv,
  * Name: rptun_register_device
  ****************************************************************************/
 
-static int rptun_register_device(FAR struct virtio_device *vdev)
+static int rptun_register_device(FAR struct rptun_priv_s *priv,
+                                 FAR struct virtio_device *vdev)
 {
   int ret = -ENODEV;
 
+  if (vdev->id.device == VIRTIO_ID_RPMSG)
+    {
+      ret = rpmsg_virtio_probe_cpuname(vdev, RPTUN_GET_CPUNAME(priv->dev));
+      if (ret < 0)
+        {
+          rptunerr("rpmsg_virtio_probe failed, ret=%d\n", ret);
+        }
+    }
+  else
 #ifdef CONFIG_DRIVERS_VIRTIO
   if (vdev->role == VIRTIO_DEV_DRIVER)
     {
@@ -572,15 +582,6 @@ static int rptun_register_device(FAR struct virtio_device 
*vdev)
     }
   else
 #endif
-  if (vdev->id.device == VIRTIO_ID_RPMSG)
-    {
-      ret = rpmsg_virtio_probe(vdev);
-      if (ret < 0)
-        {
-          rptunerr("rpmsg_virtio_probe failed, ret=%d\n", ret);
-        }
-    }
-  else
     {
       rptunerr("virtio device id = %"PRIu32" not supported\n",
                vdev->id.device);
@@ -595,6 +596,11 @@ static int rptun_register_device(FAR struct virtio_device 
*vdev)
 
 static void rptun_unregister_device(FAR struct virtio_device *vdev)
 {
+  if (vdev->id.device == VIRTIO_ID_RPMSG)
+    {
+      rpmsg_virtio_remove(vdev);
+    }
+  else
 #ifdef CONFIG_DRIVERS_VIRTIO
   if (vdev->role == VIRTIO_DEV_DRIVER)
     {
@@ -609,9 +615,9 @@ static void rptun_unregister_device(FAR struct 
virtio_device *vdev)
     }
   else
 #endif
-  if (vdev->id.device == VIRTIO_ID_RPMSG)
     {
-      rpmsg_virtio_remove(vdev);
+      rptunerr("virtio device id = %"PRIu32" not supported\n",
+               vdev->id.device);
     }
 }
 
@@ -670,7 +676,7 @@ static int rptun_create_devices(FAR struct rptun_priv_s 
*priv)
           goto err;
         }
 
-      ret = rptun_register_device(vdev);
+      ret = rptun_register_device(priv, vdev);
       if (ret < 0)
         {
           rptunerr("rptun_register_device failed, ret=%d i=%d\n", ret, i);
diff --git a/include/nuttx/rpmsg/rpmsg_virtio.h 
b/include/nuttx/rpmsg/rpmsg_virtio.h
index 5093ec43e7f..94792501448 100644
--- a/include/nuttx/rpmsg/rpmsg_virtio.h
+++ b/include/nuttx/rpmsg/rpmsg_virtio.h
@@ -51,6 +51,8 @@ extern "C"
  * Public Function Prototypes
  ****************************************************************************/
 
+int rpmsg_virtio_probe_cpuname(FAR struct virtio_device *vdev,
+                               FAR const char *cpuname);
 int rpmsg_virtio_probe(FAR struct virtio_device *vdev);
 void rpmsg_virtio_remove(FAR struct virtio_device *vdev);
 

Reply via email to