From nobody Mon Feb 9 17:58:14 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27FA03346A0; Fri, 30 Jan 2026 10:06:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769767617; cv=none; b=gASYp/7vceUYCJQUdEAq2qwXgSB/WsSCszZNxOvgquO6pO3ena59spGJ6pwGluUiahLEdEzjiXhOHfzQ58qjjl3w7454GoFk/+5RXyeo50fCAHxR8zhE5dXKO8q3dLmVIbJl4Wkv3sziDCy3frvf4SvDaKro80kYyyWH7F0JTRo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769767617; c=relaxed/simple; bh=85W6n240JRf0BCSsqd1Fe1PozjJU5RPZly+V0LpXUg4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=M0JGztFpvOmOhhXFem4HIeqNGkrs7eIfniBlHH90tAKMSgJxGhOtmv3l2lqQLmXzr+pNrPL4VdAV4NHZUTdK4H/APTBNrgkn42sOnvrc4xOa4hyWzz+XyxhsRkH3Mklh/szeD6R63kc+o8pXDXsAT29fGeEVE27/LlFrr44hDB4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gultT6oB; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gultT6oB" Received: by smtp.kernel.org (Postfix) with ESMTPS id D8409C116D0; Fri, 30 Jan 2026 10:06:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1769767616; bh=85W6n240JRf0BCSsqd1Fe1PozjJU5RPZly+V0LpXUg4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=gultT6oByquvkPMbK55C/QsmZsZkVPENyF5kR0PFEJIX/utQiiZR5nHkK4f5Hazny S991/UosY5IbmBVFb258hq/3RK8fyiRYERl2ZUQNHRi/k0lxGUVg6Qiam0WLiKDRM6 FT/iNCdJwGxwrNJkr4WQD7IohZ2nkmkRJqs0YMKQ7/mjbF9nZZiXFnGYgc03eDRROF /4v1m6iDUKk/0mkR8uabXFrhJBi4UvXYe5VvThgDtYy+mRKrkXd1/WgU/xNixP9cDP 1yCVnosLj285rjIqRL5KFW0Z4IRzk1nwPypZ5ERebV2qDgoKgSzQIaVR72QPOUxpku rtI/UnkIWJdXA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB72CD49C93; Fri, 30 Jan 2026 10:06:56 +0000 (UTC) From: Rodrigo Alencar via B4 Relay Date: Fri, 30 Jan 2026 10:06:43 +0000 Subject: [PATCH v6 2/8] iio: core: add fixed point parsing with 64-bit parts Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260130-adf41513-iio-driver-v6-2-cf46239026bc@analog.com> References: <20260130-adf41513-iio-driver-v6-0-cf46239026bc@analog.com> In-Reply-To: <20260130-adf41513-iio-driver-v6-0-cf46239026bc@analog.com> To: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-doc@vger.kernel.org Cc: Jonathan Cameron , David Lechner , Andy Shevchenko , Lars-Peter Clausen , Michael Hennerich , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet , Rodrigo Alencar X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1769767615; l=7786; i=rodrigo.alencar@analog.com; s=default; h=from:subject:message-id; bh=cUyqu8aKsZMI5PrdQO6dpsnsWP9WegoLugfDe7Kurh0=; b=W/ymPXZwv2YNjH/6T70pdls1+BOe4RqxugS/ofXxdTmPTq+oAlly3Bpy9qk0xoT/Xv84I0bMR w5ELBSqzeR9CzK8dY+jt+mJEwxrzShCK1U9ANXjl1kk+lyMbAr7a3P7 X-Developer-Key: i=rodrigo.alencar@analog.com; a=ed25519; pk=ULeHbgU/OYh/PG/4anHDfLgldFItQHAhOktYRVLMFRo= X-Endpoint-Received: by B4 Relay for rodrigo.alencar@analog.com/default with auth_id=561 X-Original-From: Rodrigo Alencar Reply-To: rodrigo.alencar@analog.com From: Rodrigo Alencar Add iio_str_to_fixpoint64() function that leverages simple_strtoull() to parse numbers from a string. A helper function __iio_str_to_fixpoint64() replaces __iio_str_to_fixpoint() implementation, extending its usage for 64-bit fixed-point parsing. Signed-off-by: Rodrigo Alencar --- drivers/iio/industrialio-core.c | 211 ++++++++++++++++++++++++++++++------= ---- include/linux/iio/iio.h | 2 + 2 files changed, 163 insertions(+), 50 deletions(-) diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-cor= e.c index 3115d59c1372..37e9ed6b659b 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -881,6 +882,136 @@ static ssize_t iio_read_channel_info_avail(struct dev= ice *dev, } } =20 +/** + * iio_safe_strntou64() - Parse u64 from string checking for overflow safe= ty + * @str: The string to parse + * @endp: output pointer to the end parsing position + * @result: parsed value + * @max_chars: maximum number of digit characters to read + * + * This function is used in fixed-point parsing and it iterates over a con= st + * char array. It might duplicate behavior of simple_strtoull() or kstrtou= ll(), + * but those have their own limitations: + * - simple_strtoull() is not overflow-safe and its usage is discouraged; + * - kstrtoull() is safe, but requires termination and it would required a= copy + * of the string to a temporary buffer. + * + * The implementation of this function is similar to _parse_integer_limit() + * available in lib/kstrtox.h, but that header/function is not available t= o be + * used in kernel modules. Hence, this implementation may need to change or + * removed to reuse a new suitable helper that is properly exposed. + * + * Returns: + * number of parsed characters on success, -ERANGE on overflow + */ +static ssize_t iio_safe_strntou64(const char *str, const char **endp, + u64 *result, size_t max_chars) +{ + u64 digit, acc =3D 0; + ssize_t idx =3D 0; + + while (isdigit(str[idx]) && idx < max_chars) { + digit =3D str[idx] - '0'; + if (unlikely(acc & (~0ull << 60))) { + if (check_mul_overflow(acc, 10, &acc) || + check_add_overflow(acc, digit, &acc)) + return -ERANGE; + } else { + acc =3D acc * 10 + digit; + } + idx++; + } + + *endp =3D str + idx; + *result =3D acc; + return idx; +} + +/** + * __iio_str_to_fixpoint64() - 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 + * + * This variant uses 64-bit integers for both integer and fractional parts. + * Parsed positive values greater than S64_MAX are returned as-is. Parsed + * negative values less than S64_MIN are treated as range error, so -ERANG= E is + * returned. + * + * Returns: + * 0 on success, or a negative error code if the string could not be parse= d. + */ +static int __iio_str_to_fixpoint64(const char *str, u64 fract_mult, + s64 *integer, s64 *fract, bool scale_db) +{ + u64 i =3D 0, f =3D 0; + int ret, precision =3D ffs(fract_mult); + bool negative =3D false; + + if (precision > 20) /* ceil(log10(U64_MAX)) =3D 20 */ + return -EINVAL; + + if (str[0] =3D=3D '-') { + negative =3D true; + str++; + } else if (str[0] =3D=3D '+') { + str++; + } + + ret =3D iio_safe_strntou64(str, &str, &i, SIZE_MAX); + if (ret < 0) + return ret; + + if (precision && *str =3D=3D '.') { + str++; /* skip decimal point */ + ret =3D iio_safe_strntou64(str, &str, &f, precision); + if (ret < 0) + return ret; + + if (ret < precision) /* scale up */ + f *=3D int_pow(10, precision - ret); + + while (isdigit(*str)) /* truncate: ignore further digits */ + str++; + } + + if (!ret) + return -EINVAL; + + if (scale_db) { + /* Ignore the dB suffix */ + if (!strncmp(str, " dB", sizeof(" dB") - 1)) + str +=3D sizeof(" dB") - 1; + else if (!strncmp(str, "dB", sizeof("dB") - 1)) + str +=3D sizeof("dB") - 1; + } + + if (*str =3D=3D '\n') + str++; + + if (*str !=3D '\0') + return -EINVAL; + + if (negative) { + if (i) { + if (i > (u64)S64_MIN) + return -ERANGE; + i =3D -i; + } else { + if (f > (u64)S64_MIN) + return -ERANGE; + f =3D -f; + } + } + + *integer =3D i; + *fract =3D f; + + return 0; +} + /** * __iio_str_to_fixpoint() - Parse a fixed-point number from a string * @str: The string to parse @@ -895,63 +1026,43 @@ static ssize_t iio_read_channel_info_avail(struct de= vice *dev, static int __iio_str_to_fixpoint(const char *str, int fract_mult, int *integer, int *fract, bool scale_db) { - int i =3D 0, f =3D 0; - bool integer_part =3D true, negative =3D false; + s64 integer64, fract64; + int ret; =20 - if (fract_mult =3D=3D 0) { - *fract =3D 0; + ret =3D __iio_str_to_fixpoint64(str, fract_mult, &integer64, &fract64, + scale_db); + if (ret) + return ret; =20 - return kstrtoint(str, 0, integer); - } + if (integer64 < INT_MIN || integer64 > UINT_MAX || + fract64 < INT_MIN || fract64 > UINT_MAX) + return -ERANGE; =20 - if (str[0] =3D=3D '-') { - negative =3D true; - str++; - } else if (str[0] =3D=3D '+') { - str++; - } - - while (*str) { - if ('0' <=3D *str && *str <=3D '9') { - if (integer_part) { - i =3D i * 10 + *str - '0'; - } else { - f +=3D fract_mult * (*str - '0'); - fract_mult /=3D 10; - } - } else if (*str =3D=3D '\n') { - if (*(str + 1) =3D=3D '\0') - break; - return -EINVAL; - } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) { - /* Ignore the dB suffix */ - str +=3D sizeof(" dB") - 1; - continue; - } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) { - /* Ignore the dB suffix */ - str +=3D sizeof("dB") - 1; - continue; - } else if (*str =3D=3D '.' && integer_part) { - integer_part =3D false; - } else { - return -EINVAL; - } - str++; - } - - if (negative) { - if (i) - i =3D -i; - else - f =3D -f; - } - - *integer =3D i; - *fract =3D f; + *integer =3D integer64; + *fract =3D fract64; =20 return 0; } =20 +/** + * iio_str_to_fixpoint64() - 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 + * + * This variant uses 64-bit integers for both integer and fractional parts. + * + * Returns: + * 0 on success, or a negative error code if the string could not be parse= d. + */ +int iio_str_to_fixpoint64(const char *str, u64 fract_mult, s64 *integer, + s64 *fract) +{ + return __iio_str_to_fixpoint64(str, fract_mult, integer, fract, false); +} +EXPORT_SYMBOL_GPL(iio_str_to_fixpoint64); + /** * iio_str_to_fixpoint() - Parse a fixed-point number from a string * @str: The string to parse diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h index a9ecff191bd9..cb30d153465a 100644 --- a/include/linux/iio/iio.h +++ b/include/linux/iio/iio.h @@ -1055,6 +1055,8 @@ int iio_active_scan_mask_index(struct iio_dev *indio_= dev); =20 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals= ); =20 +int iio_str_to_fixpoint64(const char *str, u64 fract_mult, s64 *integer, + s64 *fract); int iio_str_to_fixpoint(const char *str, int fract_mult, int *integer, int *fract); =20 --=20 2.43.0