Skip to content

Commit 6b92ba0

Browse files
larsclausenjic23
authored andcommitted
iio: __iio_format_value(): Convert to sysfs_emit_at()
sysfs_emit() is preferred over raw s*printf() for sysfs attributes since it knows about the sysfs buffer specifics and has some built-in sanity checks. Convert __iio_format_value() and related functions to use this new interface. This conversion involves changing the signature of __iio_format_value() so that it similar to sysfs_emit_at() and takes the buffers start address and an offset where to write within the buffer. Signed-off-by: Lars-Peter Clausen <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jonathan Cameron <[email protected]>
1 parent 0207483 commit 6b92ba0

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

drivers/iio/industrialio-core.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ int iio_read_mount_matrix(struct device *dev, const char *propname,
623623
}
624624
EXPORT_SYMBOL(iio_read_mount_matrix);
625625

626-
static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
626+
static ssize_t __iio_format_value(char *buf, size_t offset, unsigned int type,
627627
int size, const int *vals)
628628
{
629629
int tmp0, tmp1;
@@ -632,52 +632,53 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
632632

633633
switch (type) {
634634
case IIO_VAL_INT:
635-
return scnprintf(buf, len, "%d", vals[0]);
635+
return sysfs_emit_at(buf, offset, "%d", vals[0]);
636636
case IIO_VAL_INT_PLUS_MICRO_DB:
637637
scale_db = true;
638638
fallthrough;
639639
case IIO_VAL_INT_PLUS_MICRO:
640640
if (vals[1] < 0)
641-
return scnprintf(buf, len, "-%d.%06u%s", abs(vals[0]),
642-
-vals[1], scale_db ? " dB" : "");
641+
return sysfs_emit_at(buf, offset, "-%d.%06u%s",
642+
abs(vals[0]), -vals[1],
643+
scale_db ? " dB" : "");
643644
else
644-
return scnprintf(buf, len, "%d.%06u%s", vals[0], vals[1],
645-
scale_db ? " dB" : "");
645+
return sysfs_emit_at(buf, offset, "%d.%06u%s", vals[0],
646+
vals[1], scale_db ? " dB" : "");
646647
case IIO_VAL_INT_PLUS_NANO:
647648
if (vals[1] < 0)
648-
return scnprintf(buf, len, "-%d.%09u", abs(vals[0]),
649-
-vals[1]);
649+
return sysfs_emit_at(buf, offset, "-%d.%09u",
650+
abs(vals[0]), -vals[1]);
650651
else
651-
return scnprintf(buf, len, "%d.%09u", vals[0], vals[1]);
652+
return sysfs_emit_at(buf, offset, "%d.%09u", vals[0],
653+
vals[1]);
652654
case IIO_VAL_FRACTIONAL:
653655
tmp2 = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
654656
tmp1 = vals[1];
655657
tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1);
656658
if ((tmp2 < 0) && (tmp0 == 0))
657-
return snprintf(buf, len, "-0.%09u", abs(tmp1));
659+
return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
658660
else
659-
return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
661+
return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
662+
abs(tmp1));
660663
case IIO_VAL_FRACTIONAL_LOG2:
661664
tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
662665
tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1);
663666
if (tmp0 == 0 && tmp2 < 0)
664-
return snprintf(buf, len, "-0.%09u", abs(tmp1));
667+
return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
665668
else
666-
return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
669+
return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
670+
abs(tmp1));
667671
case IIO_VAL_INT_MULTIPLE:
668672
{
669673
int i;
670674
int l = 0;
671675

672-
for (i = 0; i < size; ++i) {
673-
l += scnprintf(&buf[l], len - l, "%d ", vals[i]);
674-
if (l >= len)
675-
break;
676-
}
676+
for (i = 0; i < size; ++i)
677+
l += sysfs_emit_at(buf, offset + l, "%d ", vals[i]);
677678
return l;
678679
}
679680
case IIO_VAL_CHAR:
680-
return scnprintf(buf, len, "%c", (char)vals[0]);
681+
return sysfs_emit_at(buf, offset, "%c", (char)vals[0]);
681682
default:
682683
return 0;
683684
}
@@ -701,11 +702,11 @@ ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
701702
{
702703
ssize_t len;
703704

704-
len = __iio_format_value(buf, PAGE_SIZE, type, size, vals);
705+
len = __iio_format_value(buf, 0, type, size, vals);
705706
if (len >= PAGE_SIZE - 1)
706707
return -EFBIG;
707708

708-
return len + sprintf(buf + len, "\n");
709+
return len + sysfs_emit_at(buf, len, "\n");
709710
}
710711
EXPORT_SYMBOL_GPL(iio_format_value);
711712

@@ -763,22 +764,21 @@ static ssize_t iio_format_list(char *buf, const int *vals, int type, int length,
763764
break;
764765
}
765766

766-
len = scnprintf(buf, PAGE_SIZE, prefix);
767+
len = sysfs_emit(buf, prefix);
767768

768769
for (i = 0; i <= length - stride; i += stride) {
769770
if (i != 0) {
770-
len += scnprintf(buf + len, PAGE_SIZE - len, " ");
771+
len += sysfs_emit_at(buf, len, " ");
771772
if (len >= PAGE_SIZE)
772773
return -EFBIG;
773774
}
774775

775-
len += __iio_format_value(buf + len, PAGE_SIZE - len, type,
776-
stride, &vals[i]);
776+
len += __iio_format_value(buf, len, type, stride, &vals[i]);
777777
if (len >= PAGE_SIZE)
778778
return -EFBIG;
779779
}
780780

781-
len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n", suffix);
781+
len += sysfs_emit_at(buf, len, "%s\n", suffix);
782782

783783
return len;
784784
}

0 commit comments

Comments
 (0)