linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v5] tools: iio: iio_generic_buffer ensure alignment
@ 2023-10-02 12:54 Matti Vaittinen
  2023-10-03  9:46 ` Matti Vaittinen
  0 siblings, 1 reply; 2+ messages in thread
From: Matti Vaittinen @ 2023-10-02 12:54 UTC (permalink / raw)
  To: Matti Vaittinen, Matti Vaittinen
  Cc: Matti Vaittinen, Jonathan Cameron, Lars-Peter Clausen, linux-iio,
	linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2515 bytes --]

The iio_generic_buffer can return garbage values when the total size of
scan data is not a multiple of the largest element in the scan. This can be
demonstrated by reading a scan, consisting, for example of one 4-byte and
one 2-byte element, where the 4-byte element is first in the buffer.

The IIO generic buffer code does not take into account the last two
padding bytes that are needed to ensure that the 4-byte data for next
scan is correctly aligned.

Add the padding bytes required to align the next sample with the scan size.

Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
Fixes: e58537ccce73 ("staging: iio: update example application.")

---
Revision history
v4 => v5:
 - drop unnecessary comment.
 - drop all other patches as those were already applied.
 - add Fixes-tag.
v3 => v4:
 - drop extra print and TODO coment
 - add comment clarifying alignment sizes
---
 tools/iio/iio_generic_buffer.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/tools/iio/iio_generic_buffer.c b/tools/iio/iio_generic_buffer.c
index 44bbf80f0cfd..c1c037ee0071 100644
--- a/tools/iio/iio_generic_buffer.c
+++ b/tools/iio/iio_generic_buffer.c
@@ -54,9 +54,12 @@ enum autochan {
 static unsigned int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
 {
 	unsigned int bytes = 0;
-	int i = 0;
+	int i = 0, max = 0;
+	unsigned int misalignment;
 
 	while (i < num_channels) {
+		if (channels[i].bytes > max)
+			max = channels[i].bytes;
 		if (bytes % channels[i].bytes == 0)
 			channels[i].location = bytes;
 		else
@@ -66,6 +69,14 @@ static unsigned int size_from_channelarray(struct iio_channel_info *channels, in
 		bytes = channels[i].location + channels[i].bytes;
 		i++;
 	}
+	/*
+	 * We wan't the data in next sample to also be properly aligned so
+	 * we'll add padding at the end if needed. Adding padding only
+	 * works for channel data which size is 2^n bytes.
+	 */
+	misalignment = bytes % max;
+	if (misalignment)
+		bytes += max - misalignment;
 
 	return bytes;
 }

base-commit: 0bb80ecc33a8fb5a682236443c1e740d5c917d1d
-- 
2.41.0


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v5] tools: iio: iio_generic_buffer ensure alignment
  2023-10-02 12:54 [PATCH v5] tools: iio: iio_generic_buffer ensure alignment Matti Vaittinen
@ 2023-10-03  9:46 ` Matti Vaittinen
  0 siblings, 0 replies; 2+ messages in thread
From: Matti Vaittinen @ 2023-10-03  9:46 UTC (permalink / raw)
  To: Matti Vaittinen
  Cc: Jonathan Cameron, Lars-Peter Clausen, linux-iio, linux-kernel

On 10/2/23 15:54, Matti Vaittinen wrote:
> The iio_generic_buffer can return garbage values when the total size of
> scan data is not a multiple of the largest element in the scan. This can be
> demonstrated by reading a scan, consisting, for example of one 4-byte and
> one 2-byte element, where the 4-byte element is first in the buffer.
> 
> The IIO generic buffer code does not take into account the last two
> padding bytes that are needed to ensure that the 4-byte data for next
> scan is correctly aligned.
> 
> Add the padding bytes required to align the next sample with the scan size.
> 
> Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
> Fixes: e58537ccce73 ("staging: iio: update example application.")
> 
> ---
> Revision history
> v4 => v5:
>   - drop unnecessary comment.
>   - drop all other patches as those were already applied.
>   - add Fixes-tag.
> v3 => v4:
>   - drop extra print and TODO coment
>   - add comment clarifying alignment sizes
> ---
>   tools/iio/iio_generic_buffer.c | 13 ++++++++++++-
>   1 file changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/iio/iio_generic_buffer.c b/tools/iio/iio_generic_buffer.c
> index 44bbf80f0cfd..c1c037ee0071 100644
> --- a/tools/iio/iio_generic_buffer.c
> +++ b/tools/iio/iio_generic_buffer.c
> @@ -54,9 +54,12 @@ enum autochan {
>   static unsigned int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
>   {
>   	unsigned int bytes = 0;
> -	int i = 0;
> +	int i = 0, max = 0;
> +	unsigned int misalignment;
>   
>   	while (i < num_channels) {
> +		if (channels[i].bytes > max)
> +			max = channels[i].bytes;
>   		if (bytes % channels[i].bytes == 0)
>   			channels[i].location = bytes;
>   		else
> @@ -66,6 +69,14 @@ static unsigned int size_from_channelarray(struct iio_channel_info *channels, in
>   		bytes = channels[i].location + channels[i].bytes;
>   		i++;
>   	}
> +	/*
> +	 * We wan't the data in next sample to also be properly aligned so

I just realized I didn't fix the typo mentioned by Andy. I can re-spin 
this with wan't => want. Sorry!

> +	 * we'll add padding at the end if needed. Adding padding only
> +	 * works for channel data which size is 2^n bytes.
> +	 */
> +	misalignment = bytes % max;
> +	if (misalignment)
> +		bytes += max - misalignment;
>   
>   	return bytes;
>   }
> 
> base-commit: 0bb80ecc33a8fb5a682236443c1e740d5c917d1d

-- 
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-10-03  9:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-02 12:54 [PATCH v5] tools: iio: iio_generic_buffer ensure alignment Matti Vaittinen
2023-10-03  9:46 ` Matti Vaittinen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).