Skip to content

Commit 2ddc982

Browse files
tachicialexJonathan Cameron
authored andcommitted
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: e553f18 ("staging: iio: core: Introduce debugfs support, add support for direct register access") Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com> Signed-off-by: Alexandru Tachici <alexandru.tachici@analog.com> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 2303248 commit 2ddc982

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

‎drivers/iio/industrialio-core.c‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,14 @@ static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
301301
size_t count, loff_t *ppos)
302302
{
303303
struct iio_dev *indio_dev = file->private_data;
304-
char buf[20];
305304
unsigned val = 0;
306-
ssize_t len;
307305
int ret;
308306

307+
if (*ppos > 0)
308+
return simple_read_from_buffer(userbuf, count, ppos,
309+
indio_dev->read_buf,
310+
indio_dev->read_buf_len);
311+
309312
ret = indio_dev->info->debugfs_reg_access(indio_dev,
310313
indio_dev->cached_reg_addr,
311314
0, &val);
@@ -314,9 +317,13 @@ static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
314317
return ret;
315318
}
316319

317-
len = snprintf(buf, sizeof(buf), "0x%X\n", val);
320+
indio_dev->read_buf_len = snprintf(indio_dev->read_buf,
321+
sizeof(indio_dev->read_buf),
322+
"0x%X\n", val);
318323

319-
return simple_read_from_buffer(userbuf, count, ppos, buf, len);
324+
return simple_read_from_buffer(userbuf, count, ppos,
325+
indio_dev->read_buf,
326+
indio_dev->read_buf_len);
320327
}
321328

322329
static ssize_t iio_debugfs_write_reg(struct file *file,

‎include/linux/iio/iio.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,8 @@ struct iio_dev {
568568
#if defined(CONFIG_DEBUG_FS)
569569
struct dentry *debugfs_dentry;
570570
unsigned cached_reg_addr;
571+
char read_buf[20];
572+
unsigned int read_buf_len;
571573
#endif
572574
};
573575

0 commit comments

Comments
 (0)