On 2021/4/1 20:12, Viresh Kumar wrote:
+
+/* vhost-user-i2c definitions */
+
+#define MAX_I2C_VDEV                    (1 << 7)
+#define MAX_I2C_ADAPTER                 16

Generally speaking, 16 is big enough for most cases. But comparing with static configuration, I think it is better if we can check how many adapters in the host when doing initialization and
use that number as "MAX_I2C_ADAPTER".


+
+static VI2cAdapter *vi2c_create_adapter(int32_t bus, uint16_t client_addr[],
+                                        int32_t n_client)
+{
+    VI2cAdapter *adapter;
+    char path[20];
+    uint64_t funcs;
+    int32_t fd, i;
+
+    if (bus < 0) {
+        return NULL;
+    }
+
+    adapter = g_malloc0(sizeof(*adapter));
+    if (!adapter) {
+        g_printerr("failed to alloc adapter");
+        return NULL;
+    }
+
+    snprintf(path, sizeof(path), "/dev/i2c-%d", bus);
+    path[sizeof(path) - 1] = '\0';
+
+    fd = open(path, O_RDWR);
+    if (fd < 0) {
+        g_printerr("virtio_i2c: failed to open %s\n", path);
+        goto fail;
+    }
+
+    adapter->fd = fd;
+    adapter->bus = bus;
+
+    if (ioctl(fd, I2C_FUNCS, &funcs) < 0) {
+        g_printerr("virtio_i2c: failed to get functionality %s: %d\n", path,
+                   errno);
+        goto close_fd;
+    }
+
+    if (funcs & I2C_FUNC_I2C) {
+        adapter->smbus = false;
+    } else if (funcs & I2C_FUNC_SMBUS_WORD_DATA) {


Only I2C_FUNC_SMBUS_WORD_DATA is checked here. But in addition to it, the smbus_xfer seems support I2C_FUNC_SMBUS_BYTE, I2C_FUNC_SMBUS_BYTE_DATA. So if an adapter only
support the latter two, it will never go to smbus_xfer.



+        adapter->smbus = true;
+    } else {
+        g_printerr("virtio_i2c: invalid functionality %lx\n", funcs);
+        goto close_fd;
+    }
+



Reply via email to