Skip to contentSkip to conversation

i2c: dev: Check for I2C_FUNC_I2C before calling i2c_transfer

PATCH Applied Jean Delvare 3 participants 3 messages May 31, 2024 – Jun 26, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: dev: Check for I2C_FUNC_I2C before calling i2c_transfer

It is good practice to check that the underlying adapter supports I2C transfers before attempting them. The i2c core would eventually return an error, but it's more efficient to fail early.

---
Not sure about that one, opinions welcome.

 drivers/i2c/i2c-dev.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/i2c/i2c-dev.c +12 -0
@@ -139,6 +139,10 @@ static ssize_t i2cdev_read(struct file *
 
 	struct i2c_client *client = file->private_data;
 
+	/* Adapter must support I2C transfers */
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		return -EOPNOTSUPP;
+
 	if (count > 8192)
 		count = 8192;
 
@@ -163,6 +167,10 @@ static ssize_t i2cdev_write(struct file
 	char *tmp;
 	struct i2c_client *client = file->private_data;
 
+	/* Adapter must support I2C transfers */
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		return -EOPNOTSUPP;
+
 	if (count > 8192)
 		count = 8192;
 
@@ -238,6 +246,10 @@ static noinline int i2cdev_ioctl_rdwr(st
 	u8 __user **data_ptrs;
 	int i, res;
 
+	/* Adapter must support I2C transfers */
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		return -EOPNOTSUPP;
+
 	data_ptrs = kmalloc_array(nmsgs, sizeof(u8 __user *), GFP_KERNEL);
 	if (data_ptrs == NULL) {
 		kfree(msgs);
Signature · 2 lines
Jean Delvare
SUSE L3 Support
maintainer Reviewed-by on v1 1/1
#1 lore raw

Hi Jean,

Quoted 5 lines · Jean Delvare

It is good practice to check that the underlying adapter supports I2C transfers before attempting them. The i2c core would eventually return an error, but it's more efficient to fail early.

Looks good to me:

Thanks,
Andi