From patchwork Mon Oct 2 12:54:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13406216 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5E628E7849A for ; Mon, 2 Oct 2023 12:54:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236437AbjJBMy5 (ORCPT ); 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-Google-Smtp-Source: AGHT+IHIrrFQD0cCpusHkYjTSE7AWof8c/S3aBqCPM4xJfPxFXYN1ojCirQX5QW45ECBTDl79siiHw== 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-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org 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 => 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(-) base-commit: 0bb80ecc33a8fb5a682236443c1e740d5c917d1d 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; }