Skip to content

Commit 97ca843

Browse files
jdelvareWolfram Sang
authored andcommitted
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. Signed-off-by: Jean Delvare <[email protected]> Reviewed-by: Andi Shyti <[email protected]> Signed-off-by: Wolfram Sang <[email protected]>
1 parent d08ed10 commit 97ca843

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

drivers/i2c/i2c-dev.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,
139139

140140
struct i2c_client *client = file->private_data;
141141

142+
/* Adapter must support I2C transfers */
143+
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
144+
return -EOPNOTSUPP;
145+
142146
if (count > 8192)
143147
count = 8192;
144148

@@ -163,6 +167,10 @@ static ssize_t i2cdev_write(struct file *file, const char __user *buf,
163167
char *tmp;
164168
struct i2c_client *client = file->private_data;
165169

170+
/* Adapter must support I2C transfers */
171+
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
172+
return -EOPNOTSUPP;
173+
166174
if (count > 8192)
167175
count = 8192;
168176

@@ -238,6 +246,10 @@ static noinline int i2cdev_ioctl_rdwr(struct i2c_client *client,
238246
u8 __user **data_ptrs;
239247
int i, res;
240248

249+
/* Adapter must support I2C transfers */
250+
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
251+
return -EOPNOTSUPP;
252+
241253
data_ptrs = kmalloc_array(nmsgs, sizeof(u8 __user *), GFP_KERNEL);
242254
if (data_ptrs == NULL) {
243255
kfree(msgs);

0 commit comments

Comments
 (0)