Skip to contentSkip to conversation

iio: proximity: srf08: Replace sprintf() with sysfs_emit()

PATCH In review Maxwell Doose 3 participants 4 messages Apr 22 – Apr 24

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

PATCH v5 1/1 iio: proximity: srf08: Replace sprintf() with sysfs_emit()

Replace sprintf() function calls with sysfs_emit() and sysfs_emit_at(). While the current code is fine, sysfs_emit() is preferred over sprintf(), and will help modernize the driver.

---
v2:
- Fixed indenting of sysfs_emit call in function srf08_show_range_mm.
- Optimized return sequence in function srf08_show_sensitivity by directly returning instead of modifying a variable and then returning.

v3:
- Changed indenting of sysfs_emit() call in function srf08_show_range_mm() per Andy Shevchenko's request.
- Clarified commit message details.

v4:
- Changed sysfs_emit() call in function srf08_show_range() to be on one line to resolve issues around indentation alignment.
- Clarified commit message details again.

v5:
- Reversed decision to put sysfs_emit() call on one line per Jonathan Cameron's request.
- Fixed some of my editor's tab-spacing issues

 drivers/iio/proximity/srf08.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/iio/proximity/srf08.c +6 -9
index d7e4cc48cfbf..2ec8d0732763 100644
@@ -226,7 +226,7 @@ static int srf08_read_raw(struct iio_dev *indio_dev,
 static ssize_t srf08_show_range_mm_available(struct device *dev,
 				struct device_attribute *attr, char *buf)
 {
-	return sprintf(buf, "[0.043 0.043 11.008]\n");
+	return sysfs_emit(buf, "[0.043 0.043 11.008]\n");
 }
 
 static IIO_DEVICE_ATTR(sensor_max_range_available, S_IRUGO,
@@ -238,8 +238,8 @@ static ssize_t srf08_show_range_mm(struct device *dev,
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct srf08_data *data = iio_priv(indio_dev);
 
-	return sprintf(buf, "%d.%03d\n", data->range_mm / 1000,
-						data->range_mm % 1000);
+	return sysfs_emit(buf, "%d.%03d\n",
+			  data->range_mm / 1000, data->range_mm % 1000);
 }
 
 /*
@@ -316,10 +316,10 @@ static ssize_t srf08_show_sensitivity_available(struct device *dev,
 
 	for (i = 0; i < data->chip_info->num_sensitivity_avail; i++)
 		if (data->chip_info->sensitivity_avail[i])
-			len += sprintf(buf + len, "%d ",
+			len += sysfs_emit_at(buf, len, "%d ",
 				data->chip_info->sensitivity_avail[i]);
 
-	len += sprintf(buf + len, "\n");
+	len += sysfs_emit_at(buf, len, "\n");
 
 	return len;
 }
@@ -332,11 +332,8 @@ static ssize_t srf08_show_sensitivity(struct device *dev,
 {
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct srf08_data *data = iio_priv(indio_dev);
-	int len;
 
-	len = sprintf(buf, "%d\n", data->sensitivity);
-
-	return len;
+	return sysfs_emit(buf, "%d\n", data->sensitivity);
 }
 
 static ssize_t srf08_write_sensitivity(struct srf08_data *data,
Signature · 1 line
2.53.0
Reviewed-by on v5 1/1
#1 lore raw
Quoted 3 lines · Maxwell Doose

Replace sprintf() function calls with sysfs_emit() and sysfs_emit_at(). While the current code is fine, sysfs_emit() is preferred over sprintf(), and will help modernize the driver.

...

Quoted 3 lines

v5:
- Reversed decision to put sysfs_emit() call on one line per Jonathan Cameron's request.

Oh, I missed that. In any case I consider my suggestion is still better, it keeps driver in limits and makes the wrap on logical boundary.

Quoted 1 line

- Fixed some of my editor's tab-spacing issues

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#2 lore raw
Quoted 7 lines · On Wed, 22 Apr 2026 15:53:55 +0300 Andy Shevchenko…
Quoted 3 lines · Maxwell Doose

Replace sprintf() function calls with sysfs_emit() and sysfs_emit_at(). While the current code is fine, sysfs_emit() is preferred over sprintf(), and will help modernize the driver.

Quoted 8 lines

...

Quoted 3 lines

v5:
- Reversed decision to put sysfs_emit() call on one line per Jonathan Cameron's request.

Oh, I missed that. In any case I consider my suggestion is still better, it keeps driver in limits and makes the wrap on logical boundary.

I'm a bit lost on which of us is asking for what and can't figure it out from the patch history. Anyhow, I did make one tweak whilst applying - I think some confusion occurred as I don't think we'd disagree on this one and you called it out in an earlier review.

drivers/iio/proximity/srf08.c +1 -1
index 2ec8d0732763..92a37ba331f6 100644
@@ -317,7 +317,7 @@ static ssize_t srf08_show_sensitivity_available(struct device *dev,
        for (i = 0; i < data->chip_info->num_sensitivity_avail; i++)
                if (data->chip_info->sensitivity_avail[i])
                        len += sysfs_emit_at(buf, len, "%d ",
-                               data->chip_info->sensitivity_avail[i]);
+                                            data->chip_info->sensitivity_avail[i]);
 
        len += sysfs_emit_at(buf, len, "\n");
 

It's only just over 80 chars and flipping to

		if (!data->chip_info->sensitivity_avail[i])
			continue;
		len += sysfs_emit_at(..

doesn't seem worth the effort.

With that applied to the testing branch of iio.git.

Thanks,

Jonathan

Quoted 3 lines
Quoted 2 lines

- Fixed some of my editor's tab-spacing issues

#3 lore raw
Quoted 5 lines · Jonathan Cameron

I'm a bit lost on which of us is asking for what and can't figure it out from the patch history. Anyhow, I did make one tweak whilst applying - I think some confusion occurred as I don't think we'd disagree on this one and you called it out in an earlier review.

[snip]

Quoted 7 lines

With that applied to the testing branch of iio.git.

Thanks,

Jonathan

Glad this patch got through and we got this worked out. Looking forward to contributing more!

best regards,
maxwell