From: Rodrigo Alencar via B4 Relay <devnull+rodrigo.alencar.analog.com@kernel.org>
To: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org
Cc: Andrew Morton <akpm@linux-foundation.org>,
Jonathan Cameron <jic23@kernel.org>,
David Lechner <dlechner@baylibre.com>,
Andy Shevchenko <andy@kernel.org>,
Rodrigo Alencar <rodrigo.alencar@analog.com>
Subject: [PATCH v5 6/7] iio: core: use kstrtodec64() to parse fixed-point values
Date: Mon, 02 Nov 2026 07:41:04 -0300 [thread overview]
Message-ID: <20261102-e-notation-v5-6-d399e544a982@analog.com> (raw)
In-Reply-To: <20261102-e-notation-v5-0-d399e544a982@analog.com>
From: Rodrigo Alencar <rodrigo.alencar@analog.com>
Replace the open-coded __iio_str_to_fixpoint() parser with kstrtodec64(),
so sysfs writes of fixed-point values also accept E notation.
The fractional part only keeps the sign when the integer part is zero,
as before. The dB suffix for scale attributes is now handled by a separate
iio_str_to_fixpoint_units() helper. It strips an optional trailing newline
and the "dB" or " dB" suffix before parsing.
When there is no fractional part (fract_mult == 0, or dec_scale == 0 for
64-bit values), try base-autodetecting kstrtoll() first so hexadecimal
and octal input keeps its meaning ("010" is still 8). Fall back to
kstrtodec64() on -EINVAL to accept E notation.
As a side effect, inputs that used to be rejected for integer-only
attributes are now accepted. An invalid octal number such as "08" is
parsed as decimal 8, and a fractional value such as "1.5" is rounded
to 2.
Signed-off-by: Rodrigo Alencar <rodrigo.alencar@analog.com>
---
drivers/iio/industrialio-core.c | 177 +++++++++++++++++++++++-----------------
1 file changed, 104 insertions(+), 73 deletions(-)
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 825cf7f32059..71e9faad4049 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -995,76 +995,21 @@ static ssize_t iio_read_channel_info_avail(struct device *dev,
}
}
-/**
- * __iio_str_to_fixpoint() - Parse a fixed-point number from a string
- * @str: The string to parse
- * @fract_mult: Multiplier for the first decimal place, should be a power of 10
- * @integer: The integer part of the number
- * @fract: The fractional part of the number
- * @scale_db: True if this should parse as dB
- *
- * Returns:
- * 0 on success, or a negative error code if the string could not be parsed.
- */
-static int __iio_str_to_fixpoint(const char *str, int fract_mult,
- int *integer, int *fract, bool scale_db)
+static int iio_str_to_s64(const char *str, s64 *val)
{
- int i = 0, f = 0;
- bool integer_part = true, negative = false;
-
- if (fract_mult == 0) {
- *fract = 0;
-
- return kstrtoint(str, 0, integer);
- }
-
- if (str[0] == '-') {
- negative = true;
- str++;
- } else if (str[0] == '+') {
- str++;
- }
-
- while (*str) {
- if ('0' <= *str && *str <= '9') {
- if (integer_part) {
- int digit = *str - '0';
-
- /* Accumulate with the sign applied so INT_MIN fits */
- if (check_mul_overflow(i, 10, &i) ||
- check_add_overflow(i, negative ? -digit : digit, &i))
- return -ERANGE;
- } else {
- f += fract_mult * (*str - '0');
- fract_mult /= 10;
- }
- } else if (*str == '\n') {
- if (*(str + 1) == '\0')
- break;
- return -EINVAL;
- } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) {
- /* Ignore the dB suffix */
- str += sizeof(" dB") - 1;
- continue;
- } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) {
- /* Ignore the dB suffix */
- str += sizeof("dB") - 1;
- continue;
- } else if (*str == '.' && integer_part) {
- integer_part = false;
- } else {
- return -EINVAL;
- }
- str++;
- }
-
- if (negative && !i)
- f = -f;
-
- *integer = i;
- *fract = f;
-
- return 0;
+ int ret;
+
+ /*
+ * Keep hex/octal support for integers. E notation and decimal values
+ * result in -EINVAL from kstrtoll(), due to unexpected termination on
+ * '.' or 'e'. It falls back to kstrtodec64() for decimal fixed-point
+ * numbers with zero scale.
+ */
+ ret = kstrtoll(str, 0, val);
+ if (ret == -EINVAL)
+ return kstrtodec64(str, 0, val);
+
+ return ret;
}
/**
@@ -1074,16 +1019,98 @@ static int __iio_str_to_fixpoint(const char *str, int fract_mult,
* @integer: The integer part of the number
* @fract: The fractional part of the number
*
+ * Parses a decimal number with an optional sign and an optional E notation
+ * exponent. The string may end with a single newline. Digits beyond the
+ * precision set by @fract_mult are rounded to nearest, ties to even, so with
+ * @fract_mult = 100000 the input "0.0000025" results in 2 micro. The sign is
+ * carried by @integer, or by @fract when @integer is zero.
+ *
+ * When @fract_mult is 0, the number is an integer and @fract is set to 0.
+ * Hexadecimal ("0x10") and octal ("010") inputs are accepted with their usual
+ * meaning. Any other input is parsed as a decimal number and rounded to
+ * nearest. For example, "1e3" results in 1000 and "1.5" in 2. Leading zeros
+ * that do not form a valid octal number fall back to decimal, so "08" results
+ * in 8.
+ *
* Returns:
* 0 on success, or a negative error code if the string could not be parsed.
*/
int iio_str_to_fixpoint(const char *str, int fract_mult,
int *integer, int *fract)
{
- return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false);
+ s64 dec64;
+ int ret;
+
+ if (fract_mult) {
+ /* scale = ffs(fract_mult) = ffs(10^n) = ffs(2^n * 5^n) = n + 1 */
+ ret = kstrtodec64(str, ffs(fract_mult), &dec64);
+ if (ret)
+ return ret;
+
+ dec64 = div_s64_rem(dec64, fract_mult * 10, fract);
+ /* only carry the sign in fract if the integer is zero */
+ if (dec64)
+ *fract = abs(*fract);
+ } else {
+ ret = iio_str_to_s64(str, &dec64);
+ if (ret)
+ return ret;
+
+ *fract = 0;
+ }
+
+ if (dec64 > INT_MAX || dec64 < INT_MIN)
+ return -ERANGE;
+
+ *integer = dec64;
+
+ return 0;
}
EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
+/**
+ * iio_str_to_fixpoint_units() - Parse a fixed-point number with units from a string
+ * @str: The string to parse
+ * @fract_mult: Multiplier for the first decimal place, should be a power of 10
+ * @integer: The integer part of the number
+ * @fract: The fractional part of the number
+ * @units: The units to expect at the end of the string
+ *
+ * Parses a fixed-point/decimal number from a string, optionally with units at
+ * the end. The string should be in the format "<number>[ <units>]". For example,
+ * the input "7.2 dB" would be parsed as integer = 7 and fract = 200000 with
+ * units = "dB" and fract_mult = 100000.
+ *
+ * Returns:
+ * 0 on success, or a negative error code if the string could not be parsed.
+ */
+static int iio_str_to_fixpoint_units(const char *str, int fract_mult,
+ int *integer, int *fract,
+ const char *units)
+{
+ size_t units_len = strlen(units);
+ size_t s_len = strlen(str);
+ const char *dec_str = str;
+ char buf[64];
+
+ if (s_len && str[s_len - 1] == '\n')
+ s_len--;
+
+ if (s_len > units_len &&
+ !strncmp(str + s_len - units_len, units, units_len)) {
+ s_len -= units_len;
+ if (str[s_len - 1] == ' ')
+ s_len--;
+ if (s_len >= sizeof(buf))
+ return -EINVAL;
+ memcpy(buf, str, s_len);
+ buf[s_len] = '\0';
+ dec_str = buf;
+ }
+
+ return iio_str_to_fixpoint(dec_str, fract_mult, integer, fract);
+}
+
static ssize_t iio_write_channel_info(struct device *dev,
struct device_attribute *attr,
const char *buf,
@@ -1148,14 +1175,18 @@ static ssize_t iio_write_channel_info(struct device *dev,
if (dec_scale)
ret = kstrtodec64(buf, dec_scale, &integer64);
else
- ret = kstrtoll(buf, 0, &integer64);
+ ret = iio_str_to_s64(buf, &integer64);
if (ret)
return ret;
iio_val_s64_decompose(integer64, &integer, &fract);
} else {
- ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract,
- scale_db);
+ if (scale_db)
+ ret = iio_str_to_fixpoint_units(buf, fract_mult,
+ &integer, &fract, "dB");
+ else
+ ret = iio_str_to_fixpoint(buf, fract_mult, &integer,
+ &fract);
if (ret)
return ret;
}
--
2.43.0
next prev parent reply other threads:[~2026-11-02 10:41 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-02 10:40 [PATCH v5 0/7] Add E notation support to kstrtodec64() and IIO sysfs writes Rodrigo Alencar via B4 Relay
2026-11-02 10:40 ` [PATCH v5 1/7] lib: kstrtox: support E notation in kstrtoudec64() and kstrtodec64() Rodrigo Alencar via B4 Relay
2026-11-02 10:41 ` [PATCH v5 2/7] lib: kstrtox: round to nearest, ties to even in kstrto[u]dec64() Rodrigo Alencar via B4 Relay
2026-11-02 10:41 ` [PATCH v5 3/7] lib: kstrtox: allow a negative scale " Rodrigo Alencar via B4 Relay
2026-11-02 10:41 ` [PATCH v5 4/7] lib: test-kstrtox: add E notation tests for kstrto[u]dec64() Rodrigo Alencar via B4 Relay
2026-11-02 10:41 ` [PATCH v5 5/7] iio: test: rescale: compare scales against the precision of the output Rodrigo Alencar via B4 Relay
2026-11-02 10:41 ` Rodrigo Alencar via B4 Relay [this message]
2026-11-02 10:41 ` [PATCH v5 7/7] iio: test: add kunit tests for channel info writes Rodrigo Alencar via B4 Relay
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261102-e-notation-v5-6-d399e544a982@analog.com \
--to=devnull+rodrigo.alencar.analog.com@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rodrigo.alencar@analog.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®