Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp1915871rwb; Fri, 19 Aug 2022 11:37:27 -0700 (PDT) X-Google-Smtp-Source: AA6agR5Z1jF0JxyGNNuj5CWsPIgnmPDfrUgo5RAkEgZsKu23psEYG5toe9oTiWtGM8o8QsLWujP3 X-Received: by 2002:a65:6cc8:0:b0:3fe:2b89:cc00 with SMTP id g8-20020a656cc8000000b003fe2b89cc00mr7394841pgw.599.1660934247651; Fri, 19 Aug 2022 11:37:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660934247; cv=none; d=google.com; s=arc-20160816; b=x9PL8QftnaB/rRBAqVA7z6RT5SUYRk8X3q5BPVKkFq8sK9iMi86k1fo/JJBFwVq8Rb egUqd9WCzRQc8Sdmr04A4cgBt2pFB8K+7Ev+Gmb0x3h5xuvjUgoZJxTOBvVrgspfIPJA AooRIByiwqwBkUOHiJ5SHrq3ZSkGy0A98n5d87RG4242FKSqYIMwk1vhhG455/tljTiE DUKUeEUgTmuSlMIN9rFjFJLEoQImhKENpR4o27fru8B2anxiwtlO+KbOtLlfxIVH/XyZ IOgvRXkVaSS1SSdyDwGLlxS5mCjPazdGO4yHbY5D/B/TrB9gz0n6BDncH1fb9DozsFn4 2RHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=llEWBs4mFs5yRwjFttaJBKqOeko11F+JzyaTkTkmsIk=; b=B5JLS4j8zcJY5y00hJBoVfOJ6i1Rr4shDuBzio7EpulMsMt61J4c1fmDIJ0w2EZqJF PRSXVLfhXAGyLxBvw7E2g2Gm7bscrbrWk2r4cnLAU12p+iqm5vp7U4YLesX+5zQXWueA rbsmkZ7L+5l6ewBi3kh6SEZvCysWrXXKb5ju+xhqxkpywHIH9Q1nU98A0Kop2ASatRHh TS+MUqcfaFNeFtO7C11diJFhx5GqW/fk2swfVmoRqKvFy5Sb6rPfVzz9KVNGrj4brsAq xx7D8gLEPRiu+3QyaauNkgYA6MFnknIKnH28aUZ+wDzL70qDlBIhXhfMMnbWe65L/c0x dmSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uixDXXWk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e17-20020a056a001a9100b005251a325ea0si4549865pfv.269.2022.08.19.11.37.14; Fri, 19 Aug 2022 11:37:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uixDXXWk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350263AbiHSR4u (ORCPT + 99 others); Fri, 19 Aug 2022 13:56:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354002AbiHSR4H (ORCPT ); Fri, 19 Aug 2022 13:56:07 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2BC65E117C; Fri, 19 Aug 2022 10:38:45 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B7639617D8; Fri, 19 Aug 2022 17:38:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 53DE7C433C1; Fri, 19 Aug 2022 17:38:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660930724; bh=OKpgMPnKr9d9cTKpfOdmcjkXXNEfExuYIhIkIm7K3RQ=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=uixDXXWkvtEq9Gd/b2RZf6otlbMU4JEP/iz6Ag940SJHlUdBVfDY0VH5ysVrdmg+L zx4mu+vd7OTagl1pX06kbIqOG6r94+Rep+EBnNNhUEaIq7YpP3kJPGNFCDv1B6qO6F zKu+LVkcpyxVXINVvt3c9M2c5hOIS/S+i2gqYefwgF+X1alSoQl16XqNGxrYZfYq18 1zCf9Qdz5aCCOlrWUscIXFo7hIYXz4h+5YZDPRMuT1xg5yr41DTzb1ETVaoEhcZ0Lw cJ8qz3H20ycCUoL1odKIeaSC0e1MDB1c78CTX7JtGaIR0tBXtpDLi/nbD/U4KgVGgI R/SwKz7rl06Ng== Date: Fri, 19 Aug 2022 18:49:19 +0100 From: Jonathan Cameron To: Artur Rojek Cc: Paul Cercueil , Dmitry Torokhov , Chris Morgan , linux-mips@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org Subject: Re: [PATCH 3/4] iio: add helper function for reading channel offset in buffer Message-ID: <20220819184919.6e08ae06@jic23-huawei> In-Reply-To: <20220817105643.95710-4-contact@artur-rojek.eu> References: <20220817105643.95710-1-contact@artur-rojek.eu> <20220817105643.95710-4-contact@artur-rojek.eu> X-Mailer: Claws Mail 4.1.0 (GTK 3.24.34; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 17 Aug 2022 12:56:42 +0200 Artur Rojek wrote: > This is useful for consumers that wish to parse raw buffer data. > > Tested-by: Paul Cercueil > Signed-off-by: Artur Rojek > --- > drivers/iio/industrialio-buffer.c | 28 ++++++++++++++++++++++++++++ > include/linux/iio/buffer.h | 4 ++++ > 2 files changed, 32 insertions(+) > > diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c > index 228598b82a2f..cf23736610d9 100644 > --- a/drivers/iio/industrialio-buffer.c > +++ b/drivers/iio/industrialio-buffer.c > @@ -691,6 +691,34 @@ static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev, > return bytes; > } > > +int iio_find_channel_offset_in_buffer(struct iio_dev *indio_dev, > + const struct iio_chan_spec *chan, > + struct iio_buffer *buffer) > +{ > + int length, offset = 0; > + unsigned int si; > + > + if (chan->scan_index < 0 || > + !test_bit(chan->scan_index, buffer->scan_mask)) { > + return -EINVAL; > + } > + > + for (si = 0; si < chan->scan_index; ++si) { > + if (!test_bit(si, buffer->scan_mask)) You are walking channels that the consumer should not even know about here. I think you can establish the same easily enough from the channels it does know about. It would be fiddly if you had lots of channels (as you might be best off sorting them) but for small numbers of channels loop over the array to find lowest scan_index - compute offset due to that then move on to next one etc until you reach the scan index of the channel you want. > + continue; > + > + length = iio_storage_bytes_for_si(indio_dev, si); > + > + /* Account for channel alignment. */ > + if (offset % length) > + offset += length - (offset % length); > + offset += length; > + } > + > + return offset; > +} > +EXPORT_SYMBOL_GPL(iio_find_channel_offset_in_buffer); > + > static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev) > { > struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); > diff --git a/include/linux/iio/buffer.h b/include/linux/iio/buffer.h > index 418b1307d3f2..b1db74772e77 100644 > --- a/include/linux/iio/buffer.h > +++ b/include/linux/iio/buffer.h > @@ -16,6 +16,10 @@ enum iio_buffer_direction { > IIO_BUFFER_DIRECTION_OUT, > }; > > +int iio_find_channel_offset_in_buffer(struct iio_dev *indio_dev, > + const struct iio_chan_spec *chan, > + struct iio_buffer *buffer); > + > int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data); > > int iio_pop_from_buffer(struct iio_buffer *buffer, void *data);