[PATCH] i2c-algo-bit: Added the bus lock during a i2c transaction

From: Anindya Sundar Gayen
Date: Tue Feb 25 2025 - 07:16:58 EST


Earlier i2c-bus lock was not available to i2c-algo-bit driver
for that reason if there are back to back i2c transaction occurs
it may be possible to occur a race condition.

To avoid the race condition we added a mutex lock mechanism which will
help to protect the i2c_outb()with a proper lock.

Signed-off-by: Anindya Sundar Gayen <anindya.sg@xxxxxxxxxxx>
---
drivers/i2c/algos/i2c-algo-bit.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/i2c/algos/i2c-algo-bit.c b/drivers/i2c/algos/i2c-algo-bit.c
index eddf25b90ca8..18021dd9eef3 100644
--- a/drivers/i2c/algos/i2c-algo-bit.c
+++ b/drivers/i2c/algos/i2c-algo-bit.c
@@ -31,7 +31,7 @@
#endif /* DEBUG */

/* ----- global variables --------------------------------------------- */
-
+static DEFINE_MUTEX(i2c_bus_lock);
static int bit_test; /* see if the line-setting functions work */
module_param(bit_test, int, S_IRUGO);
MODULE_PARM_DESC(bit_test, "lines testing - 0 off; 1 report; 2 fail if stuck");
@@ -349,6 +349,8 @@ static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
int retval;
int wrcount = 0;

+ /* Aquire the lock before accessing the I2C bus */
+ mutex_lock(&i2c_bus_lock);
while (count > 0) {
retval = i2c_outb(i2c_adap, *temp);

@@ -379,6 +381,7 @@ static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
return retval;
}
}
+ mutex_unlock(&i2c_bus_lock);
return wrcount;
}

--
2.17.1