This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 50afa97d816d4deda45222ffb6b9cc807d875624 Author: wangyongrong <[email protected]> AuthorDate: Tue Aug 18 14:48:51 2026 +0800 drivers/virtio: add virtio_foreach_device() /dev/virtio is a single global node, so an ioctl aimed at one device needs a way to find it. Add virtio_foreach_device(), which calls a handler for every claimed device with a given device id while holding the bus lock. Unclaimed devices are skipped: their vdev->priv still holds the driver pointer parked by virtio_register_device(), not the driver's context. Signed-off-by: wangyongrong <[email protected]> Signed-off-by: Bowen Wang <[email protected]> --- drivers/virtio/virtio.c | 44 +++++++++++++++++++++++++++++++++++++++++++ include/nuttx/virtio/virtio.h | 29 ++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index def7abdf3ec..a347a4a8c6b 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -394,6 +394,50 @@ void virtio_register_drivers(void) UNUSED(ret); } +/**************************************************************************** + * Name: virtio_foreach_device + ****************************************************************************/ + +int virtio_foreach_device(uint32_t id, virtio_foreach_t handler, + FAR void *arg) +{ + FAR struct dq_entry_s *node; + int ret; + + DEBUGASSERT(handler != NULL); + + ret = nxmutex_lock(&g_virtio_bus.lock); + if (ret < 0) + { + return ret; + } + + dq_for_every(&g_virtio_bus.device, node) + { + FAR struct virtio_device_item_s *item = + container_of(node, struct virtio_device_item_s, node); + + /* Only a claimed device is handed out: an unclaimed one still has the + * driver pointer parked in vdev->priv by virtio_register_device(), + * not the driver's own context. + */ + + if (item->driver == NULL || item->device->id.device != id) + { + continue; + } + + ret = handler(item->device, arg); + if (ret < 0) + { + break; + } + } + + nxmutex_unlock(&g_virtio_bus.lock); + return ret; +} + /**************************************************************************** * Name: virtio_register_driver ****************************************************************************/ diff --git a/include/nuttx/virtio/virtio.h b/include/nuttx/virtio/virtio.h index 0649e64d8d0..2580ad57d37 100644 --- a/include/nuttx/virtio/virtio.h +++ b/include/nuttx/virtio/virtio.h @@ -49,6 +49,13 @@ ****************************************************************************/ #ifdef CONFIG_DRIVERS_VIRTIO +/* Called for each device visited by virtio_foreach_device(). Returning a + * negative errno stops the walk and is handed back to the caller. + */ + +typedef CODE int (*virtio_foreach_t)(FAR struct virtio_device *vdev, + FAR void *arg); + struct virtio_driver { uint32_t device; /* device id */ @@ -237,6 +244,28 @@ int virtio_unregister_device(FAR struct virtio_device *device); void virtio_register_drivers(void); +/**************************************************************************** + * Name: virtio_foreach_device + * + * Description: + * Walk every device on the virtio bus whose device id is 'id' and which a + * driver has claimed, so vdev->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 virtio_foreach_device(uint32_t id, virtio_foreach_t handler, + FAR void *arg); + /* Buffer map ops for MMIO/PCI with per-process address environments */ #ifdef CONFIG_ARCH_ADDRENV
