Skip to contentSkip to conversation

iio: st_lsm6dsx: Replace scnprintf with sysfs_emit

PATCH Applied Akshay Bansod 3 participants 3 messages Aug 11, 2025 – Aug 16, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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
index c65ad4982..d8cb4b021 100644
@@ -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
Reviewed-by on v4 1/1
#1 lore raw
Quoted 4 lines · Akshay Bansod

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.

LGTM,

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer Applied
#2 lore raw
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() with sysfs_emit_at() which is PAGE_SIZE-aware and recommended for use in sysfs.

LGTM,

Tweaked patch title to include () for the two functions mentioned. I think that just makes things a tiny bit clearer.

Applied.