iio: st_lsm6dsx: Replace scnprintf with sysfs_emit
Subsystems (2)
PATCH v4 1/1 iio: st_lsm6dsx: Replace scnprintf with sysfs_emit
Update the sysfs interface for sampling frequency and scale attributes. Replace scnprintf() with sysfs_emit_at() which is PAGE_SIZE-aware and recommended for use in sysfs.
---
changes in v4:
- reformatted as per sugessions, used ternary operator
- Link to v3: https://lore.kernel.org/linux-iio/[email protected]/
changes in v3:
- Use sysfs_emit_at(buf, len - 1, "\n") instead of directly modifying buf[len - 1] for newline termination, aligning with sysfs_emit_at() usage.
- Link to v2: https://lore.kernel.org/linux-iio/[email protected]/
changes in v2:
- Fixed indentation for line wrap
- Link to v1: https://lore.kernel.org/linux-iio/[email protected]/
Testing:
- Built the driver (st_lsm6dsx_i2c) as a module.
- Tested using i2c-stub to mock the device.
- Verified that reading sysfs attributes like sampling_frequency_available works correctly and shows no change in functionality.
--- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +7 -7
@@ -2035,10 +2035,10 @@ st_lsm6dsx_sysfs_sampling_frequency_avail(struct device *dev, odr_table = &sensor->hw->settings->odr_table[sensor->id];for (i =0 ; i < odr_table->odr_len; i++) - len +=scnprintf (buf + len,PAGE_SIZE - len,"%d.%03d " , - odr_table->odr_avl[i].milli_hz /1000 , - odr_table->odr_avl[i].milli_hz %1000 ); - buf[len -1 ] ='\n' ; + len +=sysfs_emit_at (buf, len,"%d.%03d%c" , + odr_table->odr_avl[i].milli_hz /1000 , + odr_table->odr_avl[i].milli_hz %1000 , + (i == odr_table->odr_len -1 ) ?'\n' :' ' );return len; }
@@ -2054,9 +2054,9 @@ static ssize_t st_lsm6dsx_sysfs_scale_avail(struct device *dev, fs_table = &hw->settings->fs_table[sensor->id];for (i =0 ; i < fs_table->fs_len; i++) - len +=scnprintf (buf + len,PAGE_SIZE - len,"0.%09u " , - fs_table->fs_avl[i].gain); - buf[len -1 ] ='\n' ; + len +=sysfs_emit_at (buf, len,"0.%09u%c" , + fs_table->fs_avl[i].gain, + (i == fs_table->fs_len -1 ) ?'\n' :' ' );return len; }
Signature · 1 line
2.49.0
Quoted 4 lines · Akshay Bansod
Update the sysfs interface for sampling frequency and scale attributes. Replace
scnprintf()withsysfs_emit_at()which is PAGE_SIZE-aware and recommended for use in sysfs.
LGTM,
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 9 lines · On Mon, 11 Aug 2025 21:17:18 +0200 Andy Shevchenko <[email protected]>…
Quoted 4 lines · Akshay Bansod
Update the sysfs interface for sampling frequency and scale attributes. Replace
scnprintf()withsysfs_emit_at()which is PAGE_SIZE-aware and recommended for use in sysfs.LGTM,
Reviewed-by Andy Shevchenko <[email protected]>
Tweaked patch title to include () for the two functions mentioned. I think that just makes things a tiny bit clearer.
Applied.