Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1847226rdb; Tue, 3 Oct 2023 02:58:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGUpuQtWz8/UksHwL3ReHbxW5Y92jIgtC+jRRM8Vh/59q0c1oxnwp5PsLoR3/Zvywlligzf X-Received: by 2002:a05:6808:618f:b0:3ae:2877:9b31 with SMTP id dn15-20020a056808618f00b003ae28779b31mr13201289oib.20.1696327121275; Tue, 03 Oct 2023 02:58:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696327121; cv=none; d=google.com; s=arc-20160816; b=BlrQm7Qc2BOFHgS3RurmlK+WXKxln7bVOmo2TNGZ9T/U2yZBTBfLODqdP8G9X++NUT R9COWgVO5MhrBdpp7NGLBAYLLp72KAuBPMhNRrrYAkEwSUdMF4ffjv6PmUHs8Knd5ulW J//IYGQc+ymZP+FeGgWc/NHdAPh+alIjOYu6nYvqJDYRoigSb3SYongw7IgfuvVlGqW4 rwCR1xk6rsjxfEc/bczmSPkkV6BpqcY454ajjKSbkY/WYtvuJXlsLekYDWHLq88Ro1g+ 8J7CfZVbIVOPL0nwvU/0/VWLEw2VJ1NEQfra6VaZjHJkwm4IoV0g9owSLRfxe1eGNBi3 gqUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=itn+jp9DqMJguXGxeI/EyyIrxZjnVISE01BS8qN3dyg=; fh=jC3Zr14IfogOQHFLVGA9DtjKypPgomeJbjRJgp8dR94=; b=sQfFJweSgu+6TpyLpAHtpPOIZELCwPqp/y37in/lnS6nJGh91zcBNnVw8Dys3bOoCA 4t31SjEwGg5O7Dpl2VFNDfpd7oxzTLdO44qpnh7TI6z82oMduufgZ38L0SvIJX9WAp3O ygWaK2DOG3R8ZB3mA3gxdjnTmfB6/GhDL7eOWasvLEhQ5EPWdbtc+zbt0ukWuSkSNXmF SRguYtFhrAyS9sM7eNSY+z/uJyM95JhWAj+LIFPmly3q4ynIuL8g9sA8M/gXLu2KRB/E QQ1f9AjzZFbu+YG5IU9uT4sd2EYaAl5/L1TQm2SJZ7uMj09MbXJt7PHTr+WRhJOZWQEQ bZSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=HWEHy5W0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id ot4-20020a17090b3b4400b0027739322ce2si1171147pjb.100.2023.10.03.02.58.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 02:58:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=HWEHy5W0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 72EEE80A97DC; Tue, 3 Oct 2023 02:58:38 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239808AbjJCJ6E (ORCPT + 99 others); Tue, 3 Oct 2023 05:58:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239820AbjJCJ6D (ORCPT ); Tue, 3 Oct 2023 05:58:03 -0400 Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 195E8BB; Tue, 3 Oct 2023 02:57:59 -0700 (PDT) Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-502e7d66c1eso849270e87.1; Tue, 03 Oct 2023 02:57:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696327077; x=1696931877; darn=vger.kernel.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=itn+jp9DqMJguXGxeI/EyyIrxZjnVISE01BS8qN3dyg=; b=HWEHy5W0JGmUi2jdakt+ia773lejEEEq1G5f5AojpRkhbWaGapHZs18Dfg5yxOZGuq f8qWxhhVbkeN0Or44NfLrt+wujR6MDsukW2/T9OuhCA9bkbNgDnBkS6ueEsnSfUzWhr+ dOQDkMlkpg2Pb/QyCeIpsimF54/PlcBPtbMbyTmmhNONV8Ia3MSvE0j5aH74qtH5iHYZ g/zWAY+J7hwc9AdatDVLVoVgM/sZgaAGliNNkkZqtGhobOr75lIV3MKSfqy7uZdriqSv WW7m6GfYfMNCtBbtnwkY/FKQx4G5iRqVzTGo8v/PVa39HpEPbYnbKNL2cznuAeExLgrH sI6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696327077; x=1696931877; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=itn+jp9DqMJguXGxeI/EyyIrxZjnVISE01BS8qN3dyg=; b=D+cAGrj67+lrxklQ3e1VftJZCr5O4ELXqI9EtcSXw+B5W+6cFYUS3GtFvPnfq76tT8 QuHTcQiII3U2cX2DwFE4qhUQVUJNDOrnbsGTT3+FcmN8iiHfMg6liUFpdEYSinLgRvsO V9DVRmBaJa0QHKPc1gmT3my9aAHouoEKZI6/Oemv2iH9+2u2/G8ZqUTo2XNfk2nNyaVa jdRAkQ9IRmYhsgkteV+NHLr4zt9Bg50IrKpg8M9/DGMvYeEzozVmNvZrJfC7/aGb2bEX LqipksDKNWVInGoipifWB1MRCoEbdXdQbDxLsjjSo1fcOlfeYXcvI9jEFDxWV06fte2x K/Jg== X-Gm-Message-State: AOJu0YyDBHjSofhS86xCQOfgicKblymXmjFNdDN4tLmT105jJij5Dwqo 6Ye43JjmIQCxKFQxafm2kXA= X-Received: by 2002:a05:6512:4819:b0:505:6ede:20b0 with SMTP id eo25-20020a056512481900b005056ede20b0mr8846121lfb.58.1696327076936; Tue, 03 Oct 2023 02:57:56 -0700 (PDT) Received: from dc78bmyyyyyyyyyyyyydt-3.rev.dnainternet.fi (dc78bmyyyyyyyyyyyyydt-3.rev.dnainternet.fi. [2001:14ba:16f8:1500::7]) by smtp.gmail.com with ESMTPSA id l19-20020a19c213000000b005042ae2baf8sm132270lfc.258.2023.10.03.02.57.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 02:57:55 -0700 (PDT) Date: Tue, 3 Oct 2023 12:57:47 +0300 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Jonathan Cameron , Lars-Peter Clausen , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6] tools: iio: iio_generic_buffer ensure alignment Message-ID: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="X2pRPnCTlVZaKbsj" Content-Disposition: inline X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 03 Oct 2023 02:58:38 -0700 (PDT) --X2pRPnCTlVZaKbsj Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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 Fixes: e58537ccce73 ("staging: iio: update example application.") --- Revision history v5 =3D> v6: - fix typo pointed by Andy (wan't =3D> want). v4 =3D> v5: - drop unnecessary comment. - drop all other patches as those were already applied. - add Fixes-tag. v3 =3D> 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..0d0a7a19d6f9 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 *channe= ls, int num_channels) { unsigned int bytes =3D 0; - int i =3D 0; + int i =3D 0, max =3D 0; + unsigned int misalignment; =20 while (i < num_channels) { + if (channels[i].bytes > max) + max =3D channels[i].bytes; if (bytes % channels[i].bytes =3D=3D 0) channels[i].location =3D bytes; else @@ -66,6 +69,14 @@ static unsigned int size_from_channelarray(struct iio_ch= annel_info *channels, in bytes =3D channels[i].location + channels[i].bytes; i++; } + /* + * We want 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 =3D bytes % max; + if (misalignment) + bytes +=3D max - misalignment; =20 return bytes; } base-commit: 0bb80ecc33a8fb5a682236443c1e740d5c917d1d --=20 2.41.0 --=20 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 =3D]=20 --X2pRPnCTlVZaKbsj Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAEBCAAdFiEEIx+f8wZb28fLKEhTeFA3/03aocUFAmUb5ZsACgkQeFA3/03a ocW5wggAlOggIxqFVoJD+8TnfOgo9flg0eq3Cj9dq8lTa1fKH3emYtXpdHkoRS3S pd8ZuCSFOmQ9r4ijAd4B2488fdtjf+XJPnStVO8eRDKAwgih4o+ri0p05HJzC56l Ih4fujrRzl1oDtdIJOaGISYTELPF52uvkd7Yw6O6IubgWloZYQ9jzXEQ8ZCFsvMR Q/SwRomSBroNAUtJwSWthAAteXPZ8lPFxcFUjg84sMKIqR6G9xyx0QioijnCDDwM eRDzEbKTDGFojVpFYz7ReSl5/YyICM+gHCo5ldueGi2jX86ngrh5rfYEW85qxLG6 w/nqWWm3dmqbRV4313ZfqE16iFkfiA== =EdzQ -----END PGP SIGNATURE----- --X2pRPnCTlVZaKbsj--