diff options
author | Jean Delvare <jdelvare@suse.de> | 2024-05-31 11:34:07 +0200 |
---|---|---|
committer | Wolfram Sang <wsa+renesas@sang-engineering.com> | 2024-06-26 12:45:25 +0200 |
commit | 97ca843f6ad38036472058c152fb294e5af9f147 (patch) | |
tree | 4ff427d340a520fa086d27ef77d443810b64dd18 /drivers/i2c | |
parent | d08ed10623b3cba46102cd3d569a7517cbf48e40 (diff) |
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 <jdelvare@suse.de>
Reviewed-by: Andi Shyti <andi.shyti@kernel.org>
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
Diffstat (limited to 'drivers/i2c')
-rw-r--r-- | drivers/i2c/i2c-dev.c | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index 8b7e599f1674..f4fb212b7f39 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -139,6 +139,10 @@ static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count, 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 *file, const char __user *buf, 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(struct i2c_client *client, 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); |