From 4f201a63c9d7de8e1d19eea0a51e130aa875ef0a Mon Sep 17 00:00:00 2001 From: iamkroot Date: Mon, 15 Nov 2021 21:31:51 +0530 Subject: [PATCH 1/2] Allow forcefully setting slave address Closes #59 --- CHANGELOG.md | 1 + src/ffi.rs | 10 +++++++++- src/linux.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ac56172..d2fd5dff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Versioning](https://semver.org/spec/v2.0.0.html). - Updated nix to allow both version `0.22` or `0.23`. - Add PEC support for SMBus compatible adapters +- Add `LinuxI2CDevice::force_new()` to open the device without checking if it is bound to a driver. ## [v0.5.0] - 2021-09-21 diff --git a/src/ffi.rs b/src/ffi.rs index 6d5b0fac..61a44a7e 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -153,9 +153,10 @@ pub struct i2c_rdwr_ioctl_data { mod ioctl { pub use super::i2c_rdwr_ioctl_data; pub use super::i2c_smbus_ioctl_data; - use super::{I2C_PEC, I2C_RDWR, I2C_SLAVE, I2C_SMBUS}; + use super::{I2C_PEC, I2C_RDWR, I2C_SLAVE, I2C_SLAVE_FORCE, I2C_SMBUS}; ioctl_write_int_bad!(set_i2c_slave_address, I2C_SLAVE); + ioctl_write_int_bad!(set_i2c_slave_address_force, I2C_SLAVE_FORCE); ioctl_write_int_bad!(set_smbus_pec, I2C_PEC); ioctl_write_ptr_bad!(i2c_smbus, I2C_SMBUS, i2c_smbus_ioctl_data); ioctl_write_ptr_bad!(i2c_rdwr, I2C_RDWR, i2c_rdwr_ioctl_data); @@ -168,6 +169,13 @@ pub fn i2c_set_slave_address(fd: RawFd, slave_address: u16) -> Result<(), nix::E Ok(()) } +pub fn i2c_set_slave_address_force(fd: RawFd, slave_address: u16) -> Result<(), nix::Error> { + unsafe { + ioctl::set_i2c_slave_address_force(fd, i32::from(slave_address))?; + } + Ok(()) +} + pub fn i2c_set_smbus_pec(fd: RawFd, enable: bool) -> Result<(), nix::Error> { unsafe { ioctl::set_smbus_pec(fd, i32::from(enable))?; diff --git a/src/linux.rs b/src/linux.rs index e46b03a0..67d09ff3 100644 --- a/src/linux.rs +++ b/src/linux.rs @@ -110,6 +110,27 @@ impl LinuxI2CDevice { Ok(device) } + /// Create a new I2CDevice for the specified path, without checking if the + /// device is bound to a driver + /// + /// # Safety + /// Using this can seriously confuse the original driver, and may cause all + /// future communication to perform the wrong operations and/or return wrong results. + pub unsafe fn force_new>( + path: P, + slave_address: u16, + ) -> Result { + let file = OpenOptions::new().read(true).write(true).open(path)?; + let mut device = LinuxI2CDevice { + devfile: file, + slave_address: 0, // will be set later + pec: false, + }; + device.force_set_slave_address(slave_address)?; + device.set_smbus_pec(false)?; + Ok(device) + } + /// Set the slave address for this device /// /// Typically the address is expected to be 7-bits but 10-bit addresses @@ -127,6 +148,16 @@ impl LinuxI2CDevice { Ok(()) } + /// Set the slave address for this device, even if it is already in use + /// by a driver + /// + /// This is private; use `force_new` instead. + unsafe fn force_set_slave_address(&mut self, slave_address: u16) -> Result<(), LinuxI2CError> { + ffi::i2c_set_slave_address_force(self.as_raw_fd(), slave_address)?; + self.slave_address = slave_address; + Ok(()) + } + /// Enable/Disable PEC support for this device /// /// Used only for SMBus transactions. This request only has an effect if the From c70d87a73c62707dc69f2b15905f1a115bb29fac Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Tue, 16 Nov 2021 13:18:10 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2fd5dff..b71da4ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ Versioning](https://semver.org/spec/v2.0.0.html). - Updated nix to allow both version `0.22` or `0.23`. - Add PEC support for SMBus compatible adapters -- Add `LinuxI2CDevice::force_new()` to open the device without checking if it is bound to a driver. +- Add `LinuxI2CDevice::force_new()` to open the device without checking if the address is bound to a driver. ## [v0.5.0] - 2021-09-21