Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1312467rdb; Mon, 2 Oct 2023 06:05:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGLISpkc3X+arJUOb/rhgXqRPQslGWOJJXgBjuJ+2XWekpiVr0qBJUHftkLJuoxcioCo9tL X-Received: by 2002:a17:90a:7486:b0:268:b682:23de with SMTP id p6-20020a17090a748600b00268b68223demr11216064pjk.28.1696251958713; Mon, 02 Oct 2023 06:05:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696251958; cv=none; d=google.com; s=arc-20160816; b=IhCoyEGQRuj+XYmSw3oeZALsN4TkiGQLqfEhI6NEZ9ZWr4VR3Ycm25KmWwqBRZ3VyK PbJRR/n6HHRBkp3+k2JQKKwturocYi1UeIC8oQgiP6gSuI4G99pABIR7hnTCog53b7+y J+b3Qk3depTILVPSI+8NQHzHvh+o3Lby0vM/dWHHHoJSvU2cIgV1TtZiYTVay0B1yFjP CTmCj0075sYf26SCokrg07kUytb6B5frgtB5xa7ThY6aopl6LQKeCaTD3fiimB9tiv+/ lMNVApnXT/BEd3md5n8TJm7EtYP5tnlG6mh14ODDQPtdBhwcVks2PInbt5jMczzrQmsP +scQ== 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=nZJR14f5l2U6HTkmIyT91fkWqxzAoTmODmYIX5RU5wM=; fh=1h4YjSgDDTHCV92oKY/OJnApqRtzfV+67imdyGUaLZQ=; b=R6ups0HoQGCghHB31Ci/sC164HOq8fLt3Mv3f6b2/TEd48cTgzcoV9OWxs03aWaJjQ +oyTVYRxpDcNbdvJzRR2lw9Q3I+dK5oohHBh+Y06ZlUD0J3wnWxAEelNxHnQBktaL+7W 5IAiywaBruJfS80Ia7O5YXLeU+c7xXglrLXYhbZOcq9JfBhPa6ArU20iW8kp5ZnpLskW FWY46en7XOfW/Bw9lPKt2tgIxt+6QlrSv222DBCKPcQ8G1rjOlLjKDYI3rlPhym+WlWG RRk6gOgIftOEbtsnS+IpcQ/Vm3ENoD8dMzjVTwKWA6aOheT1e5iLXqZ3/daalMrh2+OM S6sA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SZtp6GhZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id z18-20020a17090ad79200b0027681e3a57fsi7426238pju.162.2023.10.02.06.05.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 06:05:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SZtp6GhZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (Postfix) with ESMTP id E988C80D756B; Mon, 2 Oct 2023 05:55:10 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237062AbjJBMy5 (ORCPT + 99 others); Mon, 2 Oct 2023 08:54:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229986AbjJBMy4 (ORCPT ); Mon, 2 Oct 2023 08:54:56 -0400 Received: from mail-lj1-x22d.google.com (mail-lj1-x22d.google.com [IPv6:2a00:1450:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E8E5AD; Mon, 2 Oct 2023 05:54:53 -0700 (PDT) Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-2c15463ddd4so193727991fa.3; Mon, 02 Oct 2023 05:54:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696251291; x=1696856091; 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=nZJR14f5l2U6HTkmIyT91fkWqxzAoTmODmYIX5RU5wM=; b=SZtp6GhZCIc2GkzCIUiI3kQ65UDDTeXBfXscS5B86AuRk3j+cWuvsE36arPOKyxB2O 6NVFCt1I/ZKZf2qQdkA5QP2QUReuxwBd4YmHha1q1eDVLshI+7icsVY/bUZDJlm4Ol99 lZEUgG4/Im0/tJs5xnQjjUx/0oi4NoBGYN0hrv9xNVAMt61Ftk3EmedRcy9a48FpGuCO 5v4DulxZ73pJgkeOze5UMPtxIbMjUD5kypscvEnOg6Mp13K1+VMaFpIh4B8RScU/nXVw kkl/8Kf+7aCrB3vipSdOSpxGKe4bLGdbq8arh5wel4IgUI3Mb3qxnZw8N5/U1BJGjV5y d1vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696251291; x=1696856091; 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=nZJR14f5l2U6HTkmIyT91fkWqxzAoTmODmYIX5RU5wM=; b=fBYfNRNQm+QK6lv5dgR3hl0qPYYJFRyn5Tl6ZIBizxLxWWZAtWmtH7SU9ZBpjUzKVJ 2CE3jb4J0a67TbKIqzgQHdN/FXlmbL6OnuKIzpLfzE58YgZaJOJ4MLAVY8ojR8KKn0XD FeNASdV0ehRuXSycdUitqIEoRt+ucgw26aUbSOhl+mdvSDcwNK8cUcXecSNVSacFUZsd XYGy0nCzJrl2cVzTHLgEHXw773n2wRPk0xsx0xG7TTZ4o9e5wdbKLFjBhjJc0FVv2a8P qq4z9arNiBBIyiiukZo6lb2j2uZXKugZk3Q8SGYkMM8SZck4KQWo9hdmd5DKXYQ70XE8 Yiig== X-Gm-Message-State: AOJu0YyNEyDQIL1fANB3Xm+HJIl6q3fURhGiAdrMkh0SyDQtTcfzNuiy ZmjKT0MggnE80WMwdVtexVfgmmpJLr8= X-Received: by 2002:a2e:9a89:0:b0:2b6:d13a:8e34 with SMTP id p9-20020a2e9a89000000b002b6d13a8e34mr9770893lji.46.1696251291323; Mon, 02 Oct 2023 05:54:51 -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 c4-20020a2e9d84000000b002bfb1cc89b7sm5300465ljj.110.2023.10.02.05.54.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 05:54:50 -0700 (PDT) Date: Mon, 2 Oct 2023 15:54:46 +0300 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Matti Vaittinen , Jonathan Cameron , Lars-Peter Clausen , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5] 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="uWQp3c5/bnyFA+y2" 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 agentk.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 (agentk.vger.email [0.0.0.0]); Mon, 02 Oct 2023 05:55:11 -0700 (PDT) --uWQp3c5/bnyFA+y2 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 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..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 *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 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 =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 --uWQp3c5/bnyFA+y2 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAEBCAAdFiEEIx+f8wZb28fLKEhTeFA3/03aocUFAmUavYUACgkQeFA3/03a ocW3JQf8Ccbaeg1B45itnTgW6SaOwPrBzhpHVjlNyg9uKkbNiQxr9wwmvT5/KWI0 ck+U28EoP2yVWB6Z4DJH7tCDDyBSTUuuvsAz1sWgwFkAViVeb8FnESeeRQRO5t1V zhBopbMj51/gs9AtAgXzt+MhxqGtE47GHQcPmpG5SfMdOesp7fpaEzVxj6B0/ve1 sv0GpdEhAQ1F1OINpgJ/ZbUd8hB9Mhtd6dPK/K2NrI8OPZR3LrMThlMtZtHCb+Et cWg4qPvFUeNB4x8bXs9u0nVC7c+QdXXaLPKYRAT4ZwPktCeT2Hjr6Uu5945vIfKB Agqdgysla0Rg/0g63MSZ4vstA/D5Hg== =ahQD -----END PGP SIGNATURE----- --uWQp3c5/bnyFA+y2--