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

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

commit 87144e151e73faba47181ef3ccda734ad32df8c0
Author: wangyongrong <[email protected]>
AuthorDate: Tue Aug 18 14:48:51 2026 +0800

    drivers/vhost: add vhost_foreach_device()
    
    Same as virtio_foreach_device() on the vhost bus: call a handler for
    every probed device with a given device id while holding the bus lock.
    Deferred devices are skipped, they have no driver context yet.
    
    Signed-off-by: wangyongrong <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/vhost/vhost.c       | 41 +++++++++++++++++++++++++++++++++++++++++
 include/nuttx/vhost/vhost.h | 29 +++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 95cddb40121..388a20f3b24 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -418,6 +418,47 @@ int vhost_get_vq_buffers_pa(FAR struct virtqueue *vq,
   return head;
 }
 
+/****************************************************************************
+ * Name: vhost_foreach_device
+ ****************************************************************************/
+
+int vhost_foreach_device(uint32_t id, vhost_foreach_t handler,
+                         FAR void *arg)
+{
+  FAR struct vhost_device_item_s *item;
+  int ret;
+
+  DEBUGASSERT(handler != NULL);
+
+  ret = nxmutex_lock(&g_vhost_bus.lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  /* Only the probed list is walked: a deferred device has no driver yet, so
+   * hdev->priv does not hold a driver context.
+   */
+
+  dq_for_every_entry(&g_vhost_bus.device, item,
+                     struct vhost_device_item_s, node)
+    {
+      if (item->driver == NULL || item->device->id.device != id)
+        {
+          continue;
+        }
+
+      ret = handler(item->device, arg);
+      if (ret < 0)
+        {
+          break;
+        }
+    }
+
+  nxmutex_unlock(&g_vhost_bus.lock);
+  return ret;
+}
+
 /****************************************************************************
  * Name: vhost_register_driver
  ****************************************************************************/
diff --git a/include/nuttx/vhost/vhost.h b/include/nuttx/vhost/vhost.h
index 1c36a2c6af9..e50e6cfa9c6 100644
--- a/include/nuttx/vhost/vhost.h
+++ b/include/nuttx/vhost/vhost.h
@@ -73,6 +73,13 @@
 
 #define vhost_device            virtio_device
 
+/* Called for each device visited by vhost_foreach_device().  Returning a
+ * negative errno stops the walk and is handed back to the caller.
+ */
+
+typedef CODE int (*vhost_foreach_t)(FAR struct vhost_device *hdev,
+                                    FAR void *arg);
+
 struct vhost_driver
 {
   uint32_t           device;   /* device id */
@@ -160,6 +167,28 @@ FAR void *up_vhost_iomap(uint64_t pa, FAR size_t *avail);
 
 void vhost_register_drivers(void);
 
+/****************************************************************************
+ * Name: vhost_foreach_device
+ *
+ * Description:
+ *   Walk every device on the vhost bus whose device id is 'id' and which a
+ *   driver has claimed, so hdev->priv is that driver's context.  The bus
+ *   lock is held across the walk, which keeps a device from being removed
+ *   underneath the handler.
+ *
+ * Input Parameters:
+ *   id      - Device id to match
+ *   handler - Called once per matching device
+ *   arg     - Opaque argument passed to handler
+ *
+ * Returned Value:
+ *   Zero on success, or the first negative errno a handler returned.
+ *
+ ****************************************************************************/
+
+int vhost_foreach_device(uint32_t id, vhost_foreach_t handler,
+                         FAR void *arg);
+
 /****************************************************************************
  * Name: vhost_gpio_get_ioe
  *

Reply via email to