Commit 2ddc982a authored by Alexandru Tachici's avatar Alexandru Tachici Committed by Jonathan Cameron

iio: industrialio-core: Fix debugfs read

Currently iio_debugfs_read_reg calls debugfs_reg_access
every time it is ran. Reading the same hardware register
multiple times during the same reading of a debugfs file
can cause unintended effects.

For example for each: cat iio:device0/direct_reg_access
the file_operations.read function will be called at least
twice. First will return the full length of the string in
bytes  and the second will return 0.

This patch makes iio_debugfs_read_reg to call debugfs_reg_access
only when the user's buffer position (*ppos) is 0. (meaning
it is the beginning of a new reading of the debugfs file).

Fixes: e553f182 ("staging: iio: core: Introduce debugfs support, add support for direct register access")
Signed-off-by: default avatarAlexandru Ardelean <alexandru.ardelean@analog.com>
Signed-off-by: default avatarAlexandru Tachici <alexandru.tachici@analog.com>
Signed-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 2303248b
...@@ -301,11 +301,14 @@ static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, ...@@ -301,11 +301,14 @@ static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct iio_dev *indio_dev = file->private_data; struct iio_dev *indio_dev = file->private_data;
char buf[20];
unsigned val = 0; unsigned val = 0;
ssize_t len;
int ret; int ret;
if (*ppos > 0)
return simple_read_from_buffer(userbuf, count, ppos,
indio_dev->read_buf,
indio_dev->read_buf_len);
ret = indio_dev->info->debugfs_reg_access(indio_dev, ret = indio_dev->info->debugfs_reg_access(indio_dev,
indio_dev->cached_reg_addr, indio_dev->cached_reg_addr,
0, &val); 0, &val);
...@@ -314,9 +317,13 @@ static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, ...@@ -314,9 +317,13 @@ static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
return ret; return ret;
} }
len = snprintf(buf, sizeof(buf), "0x%X\n", val); indio_dev->read_buf_len = snprintf(indio_dev->read_buf,
sizeof(indio_dev->read_buf),
"0x%X\n", val);
return simple_read_from_buffer(userbuf, count, ppos, buf, len); return simple_read_from_buffer(userbuf, count, ppos,
indio_dev->read_buf,
indio_dev->read_buf_len);
} }
static ssize_t iio_debugfs_write_reg(struct file *file, static ssize_t iio_debugfs_write_reg(struct file *file,
......
...@@ -568,6 +568,8 @@ struct iio_dev { ...@@ -568,6 +568,8 @@ struct iio_dev {
#if defined(CONFIG_DEBUG_FS) #if defined(CONFIG_DEBUG_FS)
struct dentry *debugfs_dentry; struct dentry *debugfs_dentry;
unsigned cached_reg_addr; unsigned cached_reg_addr;
char read_buf[20];
unsigned int read_buf_len;
#endif #endif
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment