hw/i2c: Add remote-i2c-master
PATCH 1/1 hw/i2c: Add remote-i2c-master
That device allows a QEMU guest to
create a character device on the host using CUSE (Character in Userspace). This enables host userspace tools (e.g., i2c-tools) to interact with the guest's virtual I2C bus directly.
The module acts as a bridge, translating I2C commands from the host into QEMU's virtual I2C operations. This is primarily useful for developing and debugging I2C device drivers.
Key components:
- New device type remote-i2c-master
- Integration with libfuse for CUSE device management
- Asynchronous I2C command handling using bottom-half timers
--- docs/system/devices/remote-i2c-master.rst | 81 ++++ hw/i2c/Kconfig | 5 + hw/i2c/meson.build | 1 + hw/i2c/remote-i2c-master.c | 535 ++++++++++++++++++++++ hw/i2c/trace-events | 15 + include/hw/i2c/remote-i2c-master.h | 66 +++ 6 files changed, 703 insertions(+) create mode 100644 docs/system/devices/remote-i2c-master.rst create mode 100644 hw/i2c/remote-i2c-master.c create mode 100644 include/hw/i2c/remote-i2c-master.h
docs/system/devices/remote-i2c-master.rst new file +81 -0
@@ -0,0 +1,81 @@ +Remote I2C master +================= + +Overview +-------- +This module implements a virtual I2C controller device using FUSE (Filesystem in Userspace) +and CUSE (Character device in Userspace) technology. It allows userspace programs to +emulate I2C controllers that appear as real character devices in the system. + +Features +-------- +- Virtual I2C controller emulation via CUSE +- Full I2C character device interface +- Support for unrestricted IOCTL operations +- Integration with QEMU's AIO context for asynchronous operations +- Debugging support through FUSE debug mode + +Architecture +------------ +The module creates a virtual character device using CUSE that behaves like a physical I2C +controller. The device can be accessed through standard I2C tools and interfaces. + +Kernel Requirements +~~~~~~~~~~~~~~~~~~~ +- CUSE module loaded: ``sudo modprobe cuse`` +- FUSE support enabled + +Library Dependencies +~~~~~~~~~~~~~~~~~~~~ +- libfuse3 or libfuse (version 2.9.0 or higher) +- FUSE development headers + +Troubleshooting +~~~~~~~~~~~~~~~ + +CUSE_INIT Failures +^^^^^^^^^^^^^^^^^^ +If you encounter ``CUSE_INIT`` errors: + +1. Verify CUSE module is loaded: + .. code-block:: bash + + lsmod | grep cuse + sudo modprobe cuse + +2. Check permissions: + .. code-block:: bash + + # Ensure user has access to CUSE devices + ls -la /dev/cuse + +Debugging +--------- + +Enable debug output by including the debug option: + +.. code-block:: c + + char fuse_opt_debug[] = FUSE_OPT_DEBUG; + char *fuse_argv[] = { fuse_opt_dummy, fuse_opt_fore, fuse_opt_debug }; + +Examples +-------- + +Basic Usage +~~~~~~~~~~~ + +.. code-block:: bash + + ./qemu-system-arm -M ast2600-evb -device tmp105,address=0x40,bus=aspeed.i2c.bus.0 -device remote-i2c-master,i2cbus=aspeed.i2c.bus.0,devname=i2c-33 + + $ i2cget -y 33 0x40 0x2 + 0x4b + + $ i2cget -y 33 0x40 0x3 + 0x50 + +See Also +-------- +- `FUSE Documentation <https://github.com/libfuse/libfuse>` +- `Character devices in user space <https://lwn.net/Articles/308445/>`
hw/i2c/Kconfig +5 -0
@@ -49,3 +49,8 @@ config PMBUS config BCM2835_I2C bool select I2C + +config REMOTE_I2C_MASTER + bool + select I2C + default y if I2C_DEVICES
hw/i2c/meson.build +1 -0
@@ -18,4 +18,5 @@ i2c_ss.add(when: 'CONFIG_PPC4XX', if_true: files('ppc4xx_i2c.c')) i2c_ss.add(when: 'CONFIG_PCA954X', if_true: files('i2c_mux_pca954x.c')) i2c_ss.add(when: 'CONFIG_PMBUS', if_true: files('pmbus_device.c')) i2c_ss.add(when: 'CONFIG_BCM2835_I2C', if_true: files('bcm2835_i2c.c')) +i2c_ss.add(when: 'CONFIG_REMOTE_I2C_MASTER', if_true: files('remote-i2c-master.c')) system_ss.add_all(when: 'CONFIG_I2C', if_true: i2c_ss)
hw/i2c/remote-i2c-master.c new file +535 -0
535 lines not on this page · raw message
hw/i2c/trace-events +15 -0
@@ -61,3 +61,18 @@ pca954x_read_data(uint8_t value) "PCA954X read data: 0x%02x" imx_i2c_read(const char *id, const char *reg, uint64_t ofs, uint64_t value) "%s:[%s (0x%" PRIx64 ")] -> 0x%02" PRIx64 imx_i2c_write(const char *id, const char *reg, uint64_t ofs, uint64_t value) "%s:[%s (0x%" PRIx64 ")] <- 0x%02" PRIx64 + +# remote-i2c-master.c +remote_i2c_master_fuse_export(void) +remote_i2c_master_fuse_io_read(void) +remote_i2c_master_i2cdev_init(void) +remote_i2c_master_i2cdev_open(void) +remote_i2c_master_i2cdev_release(void) +remote_i2c_master_i2cdev_read(void) +remote_i2c_master_i2cdev_functional(void) "Send current master functional" +remote_i2c_master_i2cdev_address(uint32_t address) "Set slave address: 0x%x" +remote_i2c_master_i2cdev_send(uint32_t size) "Send data to slave. Size: 0x%x" +remote_i2c_master_i2cdev_receive(uint32_t size) "Received data from slave. Size: 0x%x" +remote_i2c_master_i2cdev_smbus(uint8_t state) "Received SMBus command. State: 0x%x" +remote_i2c_master_i2cdev_ioctl(int cmd) "Start, command: 0x%x" +remote_i2c_master_i2cdev_ioctl_finished(int cmd) "Finished, command: 0x%x"
include/hw/i2c/remote-i2c-master.h new file +66 -0
66 lines not on this page · raw message
Signature · 1 line
2.43.0