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 7aafb8747ef8455063f2cd8c2051f712e4873a1b
Author: qinwei1 <[email protected]>
AuthorDate: Sat Nov 14 16:52:38 2026 +0800

    drivers/virtio/virtio-i2c: register /dev/i2cN at probe
    
    A probed VirtIO I2C bus was only reachable through
    virtio_i2c_get_master(), so every board had to call i2c_register()
    itself. With CONFIG_I2C_DRIVER, register it as /dev/i2cN from probe,
    as the other VirtIO frontends do for their devices; N is the bus index,
    now taken from an atomic counter before registration so the node and
    the registry agree.
    
    Signed-off-by: qinwei1 <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 Documentation/os/drivers/special/i2c.rst |  4 ++--
 drivers/virtio/virtio-i2c.c              | 39 +++++++++++++++++++++++++++-----
 2 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/Documentation/os/drivers/special/i2c.rst 
b/Documentation/os/drivers/special/i2c.rst
index ad2147683ed..b53677df908 100644
--- a/Documentation/os/drivers/special/i2c.rst
+++ b/Documentation/os/drivers/special/i2c.rst
@@ -219,8 +219,8 @@ An I2C controller can be exposed over a VirtIO transport.
 ``CONFIG_DRIVERS_VIRTIO_I2C`` enables the frontend (client), which probes a
 VirtIO I2C adapter device (device id 34) and exposes a
 ``struct i2c_master_s`` retrievable with ``virtio_i2c_get_master(index)``.
-Board code may then pass it to device drivers or register it with
-``i2c_register()`` as ``/dev/i2cN``. ``CONFIG_DRIVERS_VHOST_I2C`` enables
+With ``CONFIG_I2C_DRIVER`` the probe also registers it as ``/dev/i2cN``.
+``CONFIG_DRIVERS_VHOST_I2C`` enables
 the device-side backend (server), which serves the request virtqueue and
 forwards transfers to the local I2C device node named in its config space.
 
diff --git a/drivers/virtio/virtio-i2c.c b/drivers/virtio/virtio-i2c.c
index 94524dcd9e6..5d9f3ebc61d 100644
--- a/drivers/virtio/virtio-i2c.c
+++ b/drivers/virtio/virtio-i2c.c
@@ -31,6 +31,7 @@
 #include <sys/endian.h>
 #include <syslog.h>
 
+#include <nuttx/atomic.h>
 #include <nuttx/i2c/i2c_master.h>
 #include <nuttx/i2c/i2c_virtio.h>
 #include <nuttx/kmalloc.h>
@@ -60,7 +61,7 @@ struct virtio_i2c_bus_s
 {
   spinlock_t        lock;
   struct dq_queue_s devices;
-  int               idx;      /* Next bus index */
+  atomic_t          idx;      /* Next bus index (atomic allocator) */
 };
 
 /****************************************************************************
@@ -437,20 +438,46 @@ static int virtio_i2c_probe(FAR struct virtio_device 
*vdev)
   virtio_set_status(vdev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
   virtqueue_enable_cb(vdev->vrings_info[0].vq);
 
-  /* Publish the lower-half so board/in-kernel code can fetch it by bus index
-   * with virtio_i2c_get_master().  Exposing it as a /dev/i2cN character
-   * device with i2c_register() is left to the board, as for any other I2C
-   * lower-half.
+  /* Assign this device's VirtIO I2C bus index up front so the /dev/i2cN
+   * character device registered below carries the same index.  The index is
+   * a monotonic atomic allocator (no lock needed); atomic_add() returns the
+   * previous value.  A failed probe simply leaves a harmless gap (it is not
+   * rolled back, which would risk handing a duplicate index to a later
+   * device).
+   */
+
+  priv->idx = atomic_add(&g_virtio_i2c_bus.idx, 1);
+
+#ifdef CONFIG_I2C_DRIVER
+  /* Expose the lower-half as a /dev/i2cN character device.  For other I2C
+   * lower-halves this is left to the board, but when the I2C character
+   * driver is present we register it here so no board code is needed (a
+   * successful probe already implies the device exists; works for
+   * dynamically discovered AMP devices too).
+   */
+
+  ret = i2c_register(&priv->i2c, priv->idx);
+  if (ret < 0)
+    {
+      vrterr("i2c_register(/dev/i2c%d) failed, ret=%d\n", priv->idx, ret);
+      virtio_reset_device(vdev);
+      virtio_delete_virtqueues(vdev);
+      goto err_with_priv;
+    }
+#endif
+
+  /* Publish the lower-half in the frontend registry so board/in-kernel code
+   * can fetch it by bus index with virtio_i2c_get_master().
    */
 
   flags = spin_lock_irqsave(&g_virtio_i2c_bus.lock);
-  priv->idx = g_virtio_i2c_bus.idx++;
   dq_addlast(&priv->node, &g_virtio_i2c_bus.devices);
   spin_unlock_irqrestore(&g_virtio_i2c_bus.lock, flags);
 
   return OK;
 
 err_with_priv:
+  vdev->priv = NULL;
   nxmutex_destroy(&priv->lock);
   kmm_free(priv);
   return ret;

Reply via email to